Questions tagged [regex-lookarounds]

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match at the current match position.

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match. Lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

3265 questions
11
votes
1 answer

How to correctly use `peek()` in Rust?

I am trying to do something simple. In a slice of u8, I want to find occurrence of two characters "\r\n". However, I cannot convert that slice into String using from_utf8 because parts of slice after "\r\n" may not be utf-8 and as far as possible I…
gabhijit
  • 3,345
  • 2
  • 23
  • 36
11
votes
2 answers

Regular expression for email masking

I am trying to write a regular expression to mask an email address. Example below. input: john.doe@example.en.com output: j*******@e*********.com I have tried the following but I just can't seem to get it working…
Randall Kwiatkowski
  • 895
  • 2
  • 12
  • 23
11
votes
1 answer

lookahead regex in nginx location

I'm trying to match /category/anything, except /category/paid in nginx location. I have the following regex, but it's not working. Google tells me that I can use lookahead in nginx. Am I doing something wrong? location ^/category(?!/paid)/ { }
Moon
  • 22,195
  • 68
  • 188
  • 269
11
votes
4 answers

What do we need Lookahead/Lookbehind Zero Width Assertions for?

I've just learned about these two concepts in more detail. I've always been good with RegEx, and it seems I've never seen the need for these 2 zero width assertions. I'm pretty sure I'm wrong, but I do not see why these constructs are needed.…
0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
11
votes
3 answers

Positive lookahead to match '/' or end of string

I'm trying to do a positive lookahead to match an object ID in a given URL, regardless of where that object ID is in the URL. The idea being to match until either a '/' or the end of the string. Here are some sample strings (bold being the ID I…
devights
  • 263
  • 1
  • 4
  • 11
10
votes
3 answers

Regex: negative look-ahead between two matches

I'm trying to build a regex somewhat like this: [match-word] ... [exclude-specific-word] ... [match-word] This seems to work with a negative look-ahead, but I'm running into a problem when I have a case like this: [match-word] ...…
Alexander Malfait
  • 2,691
  • 1
  • 23
  • 23
10
votes
1 answer

Perl 6: Lookahead with capture

I'm attempting to write a Perl 6 regex for this code-golf challenge that splits a string with the rules: Sequences of the same character with length 3 or less will be grouped together But 4 or more will result in the first two being grouped before…
Jo King
  • 590
  • 3
  • 17
10
votes
1 answer

How to do a negative lookbehind within a %r<…>-delimited regexp in Ruby?

I like the %r<…> delimiters because it makes it really easy to spot the beginning and end of the regex, and I don't have to escape any /. But it seems that they have an insurmountable limitation that other delimiters don't have? Every other…
Tyler Rick
  • 9,191
  • 6
  • 60
  • 60
10
votes
1 answer

functional difference between lookarounds and non-capture group?

I'm trying to come up with an example where positive look-around works but non-capture groups won't work, to further understand their usages. The examples I"m coming up with all work with non-capture groups as well, so I feel like I"m not fully…
Moondra
  • 4,399
  • 9
  • 46
  • 104
10
votes
2 answers

Regex - lookahead assertion

I have problem with lookahead assertion (?=). For example, I have expression: /Win(?=2000)/ It match Win, if expression is like Win2000, Win2000fgF. I have next expression: ^(?=.*\d)(?=.*[a-z]).*$ It match for digit and lower case letter, for…
luk4443
  • 2,709
  • 5
  • 23
  • 20
10
votes
2 answers

Select the next line after match regex

I'm currently using a scanning software "Drivve Image" to extract certain information from each paper. This software enables certain Regex code to be run if needed. It seems to be run with the UltraEdit Regex Engine. I get the following scanned…
R0jiv4
  • 103
  • 1
  • 1
  • 6
10
votes
2 answers

Consequences of Inserting Positive Lookbehind into Arbitrary Regex to Simulate Byte Offset

What would be the consequences of inserting a positive lookbehind for n-bytes, (?<=\C{n}), into the beginning of any arbitrary regular expression, particularly when used for replacement operations? At least within PHP, the regex match functions,…
Brian North
  • 1,398
  • 1
  • 14
  • 19
10
votes
1 answer

POSIX Regular Expressions: Excluding a word in an expression?

I am trying to create a regular expression using POSIX (Extended) Regular Expressions that I can use in my C program code. Specifically, I have come up with the following, however, I want to exclude the word "http" within the matched expressions. …
9codeMan9
  • 802
  • 6
  • 11
  • 25
9
votes
3 answers

How to use regex lookahead to limit the total length of input string

I have this regular expression and want to add the rule which limit the total length is no more than 15 chars. I saw some lookahead examples but they're not quite clear. Can you help me to modify this expression to support the new rule. ^([A-Z]+(…
AustinTX
  • 1,322
  • 4
  • 21
  • 28
9
votes
5 answers

Nested regex lookahead and lookbehind

I am having problems with the nested '+'/'-' lookahead/lookbehind in regex. Let's say that I want to change the '*' in a string with '%' and let's say that '\' escapes the next character. (Turning a regex to sql like command ^^). So the string…
bliof
  • 2,957
  • 2
  • 23
  • 39