Lookarounds in Regex
Regex lookarounds allow you to match a pattern only if it's followed or preceded by another pattern.
As the name suggests, lookarounds can be look ahead or look behind. There are four types of lookarounds: Positive Lookahead, Negatve Lookahead, Positive Lookbehind, Negative Lookbehind.
Positive Lookahead
Positive lookahead matches the pattern only if it's followed or preceded by another pattern. They are denoted by the syntax x(?=y)
, wherein it says find x that is followed by y pattern.
You can think of it as a "followed by" pattern.
The following finds any word character that is followed by a whitespace character.
Negative Lookahead
Negative lookahead is the opposite of positive lookahead. It matches the pattern only if it's not followed or preceded by another pattern. They are denoted by the syntax x(?!y)
, wherein it says to find x that is not followed by y pattern.
You can think of it as a "not followed by" pattern.
The following finds any word that is not followed by a question mark ?
character.
In the above example, \b is used for a word boundary, \w+ is used for one or more word characters, so \b\w+\b
finds the whole word. Pattern, \?
uses escape sequence \
for ?
because it is a metacharacter in regex so to find out literal ?
, we must use an escape character.
Positive Lookbehind
Positive lookbehind matches a pattern only if it is preceded by another pattern but does not include the preceding pattern in the matching result. They are denoted by the syntax (?<=y)x
, wherein it says to find x that is preceded by y.
You can think of it as a "preceded by" pattern.
The following finds any word that is not followed by a question mark ?
character.
In the above example, it finds a word that is preceded by the white space character. b\w+\b
finds the whole word and \s
is for a white space character.
Negative Lookbehind
Negative lookbehind matches a pattern only if it is not preceded by another pattern. They are denoted by the syntax (?<!y)x
, wherein it says to find x that is not preceded by y.
You can think of it as a "not preceded by" pattern.
The following finds any word that is not followed by a question mark ?
character.
In the above example, it finds a word that is not preceded by a white space character. b\w+\b
finds the whole word and \s
is for the white space character.