Skip to content

Instantly share code, notes, and snippets.

@zestime
Created March 13, 2015 10:24
Show Gist options
  • Save zestime/0997ec2a72b598c95114 to your computer and use it in GitHub Desktop.
Save zestime/0997ec2a72b598c95114 to your computer and use it in GitHub Desktop.
lookahead & lookbehind in python
>>> import re
>>> msg1 = "aaabbbccc"
>>> msg2 = "dddbbbeee"
>>> re.findall("bbb(?=ccc)", msg1) # lookahead
['bbb']
>>> re.findall("bbb(?=ccc)", msg2)
[]
>>> re.findall("bbb(?!ccc)", msg1) # negative lookahead
[]
>>> re.findall("bbb(?!ccc)", msg2)
['bbb']
>>> re.findall("(?<=aaa)bbb", msg1) # lookbehind
['bbb']
>>> re.findall("(?<=aaa)bbb", msg2)
[]
>>> re.findall("(?<!aaa)bbb", msg1) # negative lookbehind
[]
>>> re.findall("(?<!aaa)bbb", msg2)
['bbb']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment