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
import re
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
import re
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
example1 = re.search(r '(?<=[a-z])\d' ,
"geeks12" )
print (example1.group())
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
example1 = re.search( '(?<=[a-z])geeks' ,
'geeksforgeeks' )
print (example1.group())
example2 = re.search( '(?<![a-z])123' ,
'geeks123' )
print (example2)
|
Output:
geeks
None
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Feb, 2021
Like Article
Save Article