Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python – Regex Lookbehind

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Regex Lookbehind is used as an assertion in Python regular expressions(re) to determine success or failure whether the pattern is behind i.e to the right of the parser’s current position. They don’t match anything. Hence, Regex Lookbehind and lookahead are termed as a zero-width assertion.

Syntax:

# Positive lookbehind
(?<=<lookbehind_regex>)

# Positive lookahead
(?=<lookahead_regex>)

In this post we will talk about regex lookbehind.

Example 1:

Python3




# importing regex
import re
  
# Regex Lookbehind example
example = re.search(r'(?<=geeks)\w'
                    'geeksforgeeks')
  
print(example.group())
print("Pattern found from index"
      example.start(), example.end())

Output:

f
Pattern found from index 5 6

The regex lookbehind assertion (?<=geeks) specifies that what precedes before any word character(‘\w’) must be ‘geeks’ string. In this case, it’s the character ‘f’ before which ‘geeks’ string occurs.

Example 2:

Python3




# importing regex
import re
  
# Regex Lookbehind example
example = re.search(r'(?<=geeks)\d'
                    'geeksforgeeks')
print(example)

Output:

None

In the above example, the output is None because there is no decimal digit preceded by ‘geeks’ string.

Lookbehind portion is not part of the search string. They are important when you don’t want the output to return lookbehind portion present in search string but want to use it to match pattern which is preceded by a particular section. Below example will make this clear:

Example 3:

Python3




import re
  
# Using lookbehind
example1 = re.search(r'(?<=[a-z])\d',
                     "geeks12")
print(example1.group())
  
# Without using lookbehind
example2 = re.search(r'([a-z])\d',
                     "geeks12")
print(example2.group())

Output:

1
s1

Using lookbehind the output generated is ‘1’ whereas without using lookbehind the output generated is ‘s1’.  Any word character (\w) which precedes any decimal digit is consumed by regex so it doesn’t become part of the search string. 

Negative Lookbehind

Negative Lookbehind is the opposite of lookbehind. It is to assure that the search string is not preceded by <lookbehind_regex>.

Syntax:

(?<!<lookbehind_regex>) 
Negative Lookbehind

Example 4:

Python3




import re
  
# Lookbehind
example1 = re.search('(?<=[a-z])geeks'
                     'geeksforgeeks')
print(example1.group())
  
# Negative Lookbehind
example2 = re.search('(?<![a-z])123'
                     'geeks123')
  
# Output is None because 123 
# is preceded by a character i.e 's'
print(example2)

Output:

geeks
None

My Personal Notes arrow_drop_up
Last Updated : 24 Feb, 2021
Like Article
Save Article
Similar Reads
Related Tutorials