Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match if not before and after

Tags:

python

regex

How can I match 'suck' only if not part of 'honeysuckle'?

Using lookbehind and lookahead I can match suck if not 'honeysuck' or 'suckle', but it also fails to catch something like 'honeysucker'; here the expression should match, because it doesn't end in le:

re.search(r'(?<!honey)suck(?!le)', 'honeysucker')
like image 710
user3117610 Avatar asked Feb 17 '14 08:02

user3117610


1 Answers

You need to nest the lookaround assertions:

>>> import re
>>> regex = re.compile(r"(?<!honey(?=suckle))suck")
>>> regex.search("honeysuckle")
>>> regex.search("honeysucker")
<_sre.SRE_Match object at 0x00000000029B6370>
>>> regex.search("suckle")
<_sre.SRE_Match object at 0x00000000029B63D8>
>>> regex.search("suck")
<_sre.SRE_Match object at 0x00000000029B6370>

An equivalent solution would be suck(?!(?<=honeysuck)le).

like image 69
Tim Pietzcker Avatar answered Sep 19 '22 16:09

Tim Pietzcker