Regex Lookarounds
Lookarounds are used to match a pattern only if it is followed or preceded by another pattern. They are zero-width assertions, which means they don’t consume any characters in the string.
Positive Lookahead (?=)
Positive lookahead asserts that the pattern inside the lookahead must match the text following the current position.
The syntax for positive lookahead is (?=...)
.
For example, the regex pattern go(?=ogle)
matches go
only if it is followed by ogle
.
Negative Lookahead (?!)
Negative lookahead asserts that the pattern inside the lookahead must not match the text following the current position.
The syntax for negative lookahead is (?!...)
.
For example, the regex pattern go(?!ogle)
matches go
only if it is not followed by ogle
.
Positive Lookbehind (?<=)
Positive lookbehind asserts that the pattern inside the lookbehind must match the text preceding the current position.
The syntax for positive lookbehind is (?<=...)
.
For example, the regex pattern (?<=go)gle
matches gle
only if it is preceded by go
.
Negative Lookbehind (?<!)
Negative lookbehind asserts that the pattern inside the lookbehind must not match the text preceding the current position.
The syntax for negative lookbehind is (?<!...)
.
For example, the regex pattern (?<!go)gle
matches gle
only if it is not preceded by go
.