Lookahead and Lookbehind in Regular Expressions
1. Positive Lookahead (?=...)
Positive lookahead asserts that what immediately follows the current position in the string is the specified pattern. It is written as (?=...)
. The lookahead itself is not included in the match.
Example:
Pattern: foo(?=bar)
Matches: "foo" in "foobar"
Explanation: The pattern matches "foo" only if it is followed by "bar", but "bar" is not included in the match.
2. Negative Lookahead (?!...)
Negative lookahead asserts that what immediately follows the current position in the string is not the specified pattern. It is written as (?!...)
. The lookahead itself is not included in the match.
Example:
Pattern: foo(?!bar)
Matches: "foo" in "foobaz"
Explanation: The pattern matches "foo" only if it is not followed by "bar", but "baz" is not included in the match.
3. Positive Lookbehind (?<=...)
Positive lookbehind asserts that what immediately precedes the current position in the string is the specified pattern. It is written as (?<=...)
. The lookbehind itself is not included in the match.
Example:
Pattern: (?<=foo)bar
Matches: "bar" in "foobar"
Explanation: The pattern matches "bar" only if it is preceded by "foo", but "foo" is not included in the match.
4. Negative Lookbehind (?<!...)
Negative lookbehind asserts that what immediately precedes the current position in the string is not the specified pattern. It is written as (?<!...)
. The lookbehind itself is not included in the match.
Example:
Pattern: (?<!foo)bar
Matches: "bar" in "bazbar"
Explanation: The pattern matches "bar" only if it is not preceded by "foo", but "baz" is not included in the match.
5. Practical Examples
Lookahead and lookbehind are powerful tools for precise pattern matching. Here are some practical examples:
Example:
Pattern: \d+(?=\sUSD)
Text: "The price is 100 USD."
Matches: "100"
Explanation: The pattern matches a number only if it is followed by a space and "USD".
Example:
Pattern: (?<=\$)\d+
Text: "The price is $100."
Matches: "100"
Explanation: The pattern matches a number only if it is preceded by a dollar sign.