Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: regex negative lookbehind assertion not working correctly?

Reply
Thread Tools

Re: regex negative lookbehind assertion not working correctly?

 
 
MRAB
Guest
Posts: n/a
 
      03-31-2009
Gabriel Rossetti wrote:
> 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+)(?<!14522XXX9",

> "/profile.php?id=14522XXX98").groups()
> ('14522XXX9',)
>
> which should not match, but it does, then I tried this :
>

[snip]
How can '(\d+)' be capturing '14522XXX9'? '\d' matches only digits!

Anyway, your basic problem is that it initially matches '14522XXX98',
but then the lookbehind rejects that, so it backtracks and releases the
last character, giving '14522XXX9', which is not be rejected because
'14522XXX9' isn't '14522XXX98'.

Try putting a '\b' after the '\d+' to reject partial IDs.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A zero-width positive lookbehind assertion George Perl Misc 4 02-06-2010 11:30 PM
regex negative lookbehind assertion not working correctly? Gabriel Rossetti Python 0 03-31-2009 03:38 PM
Re: Confused about 'positive lookbehind assertion' Erik Jones Python 3 09-26-2007 10:02 AM
Negative Lookbehind Replacement? mail Perl 1 03-02-2004 03:14 PM
Negative Lookbehind and Wildcards Thomas F. O'Connell Perl 1 02-28-2004 01:50 PM



Advertisments