Python Regular Expression – Lookbehind

Spread the love

Positive Lookbehind –

In regular expression, the positive lookbehind matches a string if there is a specific pattern before it.

syntax –

(?<=lookbehind_regex)

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 [1]: import re

In [2]: re.findall('(?<=\$)\d+', 'The price of 1 stock of apple is $172')
Out[2]: ['172']

The \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.

syntax –

(?<!lookbehind_regex)

Now, if you only want to match with numbers that does not contains dollar signs before it then you can negative lookbehind.

In [3]: re.findall(r'\b(?<!\$)\d+\b', 'The price of 1 stock of apple is $172')
Out[3]: ['1']

The \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.

Rating: 1 out of 5.

Leave a Reply