regex negative lookbehind assertion not working correctly?

Gabriel Rossetti gabriel.rossetti at arimaz.com
Tue Mar 31 17:38:05 CEST 2009


Hello everyone,

I am trying to write a regex pattern to match an ID in a URL only if it 
is not a given ID. Here's an example, the ID not to match is 
"14522XXX98", if my URL is "/profile.php?id=14522XXX99" I want it to 
match and if it's "/profile.php?id=14522XXX98" I want it not to. I tried 
this:

 >>> re.search(r"/profile.php\?id=(\d+)(?<!14522XXX98)", 
"/profile.php?id=14522XXX98").groups()
('14522XXX9',)

which should not match, but it does, then I tried this :

 >>> re.search(r"/profile.php\?id=(\d+)(?<!14522XXX98)", 
"/profile.php?id=14522XXX99").groups()
('14522XXX99',)

which should match and it does. I then tried uring /positive lookbehind 
assertion/ instead and it does this :

 >>> re.search(r"/profile.php\?id=(\d+)(?<=14522XXX98)", 
"/profile.php?id=14522XXX98").groups()
('14522XXX98',)

which matches as it should and then I tried this :

 >>> re.search(r"/profile.php\?id=(\d+)(?<=14522XXX98)", 
"/profile.php?id=14522XXX99").groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'

which doesn't match as it should. Could someone please explain why the 
negative lookbehind assertion is not working as I understand it? Also, 
notice how the last digit of the first expression is not matched, I get 
('14522XXX9',) instead of ('14522XXX98',), why? It does on the others....

Thank you,
Gabriel



More information about the Python-list mailing list