Python – Regex Lookahead
Last Updated :
03 Jan, 2021
Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser’s current position. They don’t match anything. Hence, they are called as zero-width assertions.
Syntax:
# Positive lookahead
(?=<lookahead_regex>)
Example 1:
Python3
import re
example = re.search(r 'geeks(?=[a-z])' , "geeksforgeeks" )
print ( "Pattern:" , example.group())
print ( "Pattern found from index:" ,
example.start(), "to" ,
example.end())
|
Output:
Pattern: geeks
Pattern found from index: 0 to 5
The lookahead assertion (?=[a-z]) specifies that what follows geeks must be a lowercase alphabetic character. In this case, it’s the character f, a match is found.
Example 2:
Python3
import re
example = re.search(r 'geeks(?=[a-z])' ,
"geeks123" )
print (example)
|
Output:
None
In the above example, the output is None because the next character after geeks is 1. It is not a lowercase alphabetic character.
Lookahead portion is not part of the search string. Hence, it is termed as zero width assertion. They are important when you don’t want the output to return lookahead portion present in search string but want to use it to match pattern which is followed by a particular section. Below example will make this clear.
Example 3:
Python3
import re
example1 = re.search(r 'geeks(?=[a-z])' ,
"geeksforgeeks" )
print ( 'Using lookahead:' , example1.group())
example2 = re.search(r 'geeks([a-z])' ,
"geeksforgeeks" )
print ( 'Without using lookahead:' , example2.group())
|
Output:
Using lookahead: geeks
Without using lookahead: geeksf
Using lookahead the output generated is ‘geeks’ whereas without using lookahead the output generated is geeksf. The f is consumed by regex and it becomes part of the search string.
Negative lookahead is opposite of lookahead. It is to assure that the search string is not followed by <lookahead_regex>.
Syntax:
# Negative Lookahead
(?!<lookahead_regex>)
Example 4:
Python3
import re
example1 = re.search( 'geeks(?=[a-z])' ,
'geeksforgeeks' )
print ( 'Positive Lookahead:' , example1.group())
example2 = re.search( 'geeks(?![a-z])' ,
'geeks123' )
print ( 'Negative Lookahead:' , example2.group())
|
Output:
Positive Lookahead: geeks
Negative Lookahead: geeks
In the above example, the output is geeks because search string geeks here is not followed by lowercase letters.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...