regex negative lookbehind assertion not working correctly?

Discussion in 'Python' started by Gabriel Rossetti, Mar 31, 2009.

  1. 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
     
    Gabriel Rossetti, 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:
    725
    Gunnar Hjalmarsson
    Feb 28, 2004
  2. mail
    Replies:
    1
    Views:
    534
    Will Stranathan
    Mar 2, 2004
  3. Erik Jones
    Replies:
    3
    Views:
    484
    Duncan Booth
    Sep 26, 2007
  4. MRAB
    Replies:
    0
    Views:
    528
  5. George
    Replies:
    4
    Views:
    123
    C.DeRykus
    Feb 6, 2010
Loading...

Share This Page