Monday, August 28, 2017

Regexp Lookahead vs Lookbehind

http://www.regular-expressions.info/lookaround.html

Lookahead means look forwards (to the right)
If the regex is x(?=insert_regex_here), which is positive look ahead, that means "find an x that is followed by insert_regex_here".

If the regex is x(?!insert_regex_here), which is negative look ahead, that means "find an x that is not followed by insert_regex_here".

Lookbehind means look backwards (to the left)
If the regex is (?<=insert_regex_here)x, which is positive look behind, that means "find an x that is preceded by insert_regex_here".

If the regex is (?<!insert_regex_here)x, which is negative look behind, that means "find an x not preceded by insert_regex_here"

The above (?=   (?!   (?<=   (?<! etc are Perl regex syntax - the syntax might be slightly different depending on your flavour of regex. You can also use https://regex101.com/ to play around these two concepts in RegExp.

No comments:

Post a Comment