Re: regex negative lookbehind assertion not working correctly?

Discussion in 'Python' started by MRAB, Mar 31, 2009.

  1. MRAB

    MRAB Guest

    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+)(?<!14522XXX98)",

    > "/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.
    MRAB, Mar 31, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas F. O'Connell

    Negative Lookbehind and Wildcards

    Thomas F. O'Connell, Feb 27, 2004, in forum: Perl
    Replies:
    1
    Views:
    713
    Gunnar Hjalmarsson
    Feb 28, 2004
  2. mail
    Replies:
    1
    Views:
    522
    Will Stranathan
    Mar 2, 2004
  3. Erik Jones
    Replies:
    3
    Views:
    476
    Duncan Booth
    Sep 26, 2007
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    564
    Gabriel Rossetti
    Mar 31, 2009
  5. George
    Replies:
    4
    Views:
    109
    C.DeRykus
    Feb 6, 2010
Loading...

Share This Page