Lookahead and Lookbehind
Lookahead and lookbehind are special constructs in regular expressions that allow you to specify a pattern to match based on what comes before or after the current position in the input text.
Positive Lookahead
Positive lookahead is denoted by (?=pattern)
and is used to match a pattern only if it is followed by another pattern. For example, the regex (?=regex)python
will match the word 'python' only if it is followed by the word 'regex'.
Negative Lookahead
Negative lookahead is denoted by (?!pattern)
and is used to match a pattern only if it is not followed by another pattern. For example, the regex (?!regex)python
will match the word 'python' only if it is not followed by the word 'regex'.
Positive Lookbehind
Positive lookbehind is denoted by (?<=pattern)
and is used to match a pattern only if it is preceded by another pattern. For example, the regex (?<=python) regex
will match the word 'regex' only if it is preceded by the word 'python'.
Negative Lookbehind
Negative lookbehind is denoted by (?<!pattern)
and is used to match a pattern only if it is not preceded by another pattern. For example, the regex (?<!python) regex
will match the word 'regex' only if it is not preceded by the word 'python'.
xxxxxxxxxx
import re
# Positive Lookahead
pattern = r'(?=regex)python'
print(re.findall(pattern, 'python is a powerful regex language'))
# Negative Lookahead
pattern = r'(?!=regex)python'
print(re.findall(pattern, 'python is a powerful regex language'))
# Positive Lookbehind
pattern = r'(?<=python) regex'
print(re.findall(pattern, 'python is a powerful regex language'))
# Negative Lookbehind
pattern = r'(?<!python) regex'
print(re.findall(pattern, 'python is a powerful regex language'))