Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Regex Lookbehind

Python – Regex Lookbehind

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'(?<=Lazyroar)\w'
                    'neveropen')
  
print(example.group())
print("Pattern found from index"
      example.start(), example.end())


Output:

f
Pattern found from index 5 6

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

Example 2:

Python3




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


Output:

None

In the above example, the output is None because there is no decimal digit preceded by ‘Lazyroar’ 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',
                     "Lazyroar12")
print(example1.group())
  
# Without using lookbehind
example2 = re.search(r'([a-z])\d',
                     "Lazyroar12")
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])Lazyroar'
                     'neveropen')
print(example1.group())
  
# Negative Lookbehind
example2 = re.search('(?<![a-z])123'
                     'Lazyroar123')
  
# Output is None because 123 
# is preceded by a character i.e 's'
print(example2)


Output:

Lazyroar
None

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments