Lookaround is a feature of regular expressions that allows you to look ahead or behind the current position in a string to see if a pattern matches, without actually consuming any characters from the string.
There are two types of lookaround:
Positive lookaround ((?=pattern)) - Matches the current position in the string only if it is followed by the pattern specified in pattern.
Negative lookaround ((?!pattern)) - Matches the current position in the string only if it is not followed by the pattern specified in pattern.
Positive lookaround is useful when you want to match a pattern only if it is followed by another pattern. For example, suppose you want to match all occurrences of the word "cat" that are followed by the word "in". You can use the positive lookahead (?=in) to accomplish this:
import retext = "The cat is in the hat"pattern = r"cat(?= in)"matches = re.findall(pattern, text)print(matches) |
In this example, the regular expression cat(?= in) matches the word "cat" only if it is followed by the string " in". The re.findall() function is used to find all non-overlapping matches of the pattern in the input string text.
The output of this program is:
['cat'] |
Negative lookaround is useful when you want to match a pattern only if it is not followed by another pattern. For example, suppose you want to match all occurrences of the word "cat" that are not followed by the word "in". You can use the negative lookahead (?!in) to accomplish this:
import retext = "The cat is in the hat"pattern = r"cat(?! in)"matches = re.findall(pattern, text)print(matches) |
In this example, the regular expression cat(?! in) matches the word "cat" only if it is not followed by the string " in".
The output of this program is:
['cat', 'cat'] |
As you can see, the regular expression matches both occurrences of the word "cat" in the input string, because neither is followed by the string " in".
Lookaround can be a powerful tool for creating complex regular expressions that match specific patterns in strings. However, it can also make your regular expressions harder to read and understand, so it should be used judiciously.