Positive Lookbehind –
In regular expression, the positive lookbehind matches a string if there is a specific pattern before it.
Let’s say you have some data about stock prices and you want to find the prices of stocks but do not want to match with the number of stocks. For this you can use lookbehind in regex.
In : import re In : re.findall('(?<=\$)\d+', 'The price of 1 stock of apple is $172') Out: ['172']
\d+ says that there is one or more digit characters and
(?<=\$) says only match with the strings which has dollar sign before the digit characters. Since $ is a special character in regex we need to escape it with a backslash to match with regular $ character.
Negative lookbehind –
In regular expression, Negative lookbehind only matches with a string if there is not a specific pattern before it.
Now, if you only want to match with numbers that does not contains dollar signs before it then you can negative lookbehind.
In : re.findall(r'\b(?<!\$)\d+\b', 'The price of 1 stock of apple is $172') Out: ['1']
\b matches if a word begins or ends with the given characters. We used it to isolate words. The pattern
(?<!\$) says that the string does not starts with a dollar sign, and it contains one or more digit characters.