Bug in re module?

Discussion in 'Python' started by Ant, Oct 6, 2006.

  1. Ant

    Ant Guest

    Look at the following minimal example:

    >>> import re
    >>> p = re.compile(r":)?Test) (String)")
    >>> m = p.search("This is a Test String OK?")
    >>> m.groups()

    ('Test', 'String')

    I would have expected this to produce:

    ('String')

    since :)?...) should be a non-capturing group. From the module
    reference:

    (?:...)
    A non-grouping version of regular parentheses. Matches whatever
    regular expression is inside the parentheses, but the substring matched
    by the group cannot be retrieved after performing a match or referenced
    later in the pattern.
     
    Ant, Oct 6, 2006
    #1
    1. Advertising

  2. Ant

    Ant Guest

    Ant wrote:
    > Look at the following minimal example:

    .... (snip example that shows non-capturing group capturing)

    Note I get the same results from python versions 2.4 and 2.5.
     
    Ant, Oct 6, 2006
    #2
    1. Advertising

  3. Ant

    Just Guest

    In article <>,
    "Ant" <> wrote:

    > Ant wrote:
    > > Look at the following minimal example:

    > ... (snip example that shows non-capturing group capturing)
    >
    > Note I get the same results from python versions 2.4 and 2.5.
    >


    Try ?: instead of :?

    Just
     
    Just, Oct 6, 2006
    #3
  4. Ant wrote:
    >


    > I would have expected this to produce:
    >
    > ('String')
    >
    > since :)?...) should be a non-capturing group. From the module
    > reference:
    >
    > (?:...)


    (?:...) isn't the same thing as :)?...), though.

    </F>
     
    Fredrik Lundh, Oct 6, 2006
    #4
  5. Ant

    John Machin Guest

    Ant wrote:
    > Look at the following minimal example:
    >
    > >>> import re
    > >>> p = re.compile(r":)?Test) (String)")


    Bzzzzzt! Sorry, PBKAC.

    The correct syntax is (?:foo)
    You have :)?foo)
    which matches an optional colon followed by foo.

    Now quick kill your post before the effbot spots it :)


    > >>> m = p.search("This is a Test String OK?")
    > >>> m.groups()

    > ('Test', 'String')
    >
    > I would have expected this to produce:
    >
    > ('String')
    >
    > since :)?...) should be a non-capturing group. From the module
    > reference:
    >
    > (?:...)
    > A non-grouping version of regular parentheses. Matches whatever
    > regular expression is inside the parentheses, but the substring matched
    > by the group cannot be retrieved after performing a match or referenced
    > later in the pattern.
     
    John Machin, Oct 6, 2006
    #5
  6. Ant

    Ant Guest

    Just wrote:

    > Try ?: instead of :?


    Duh. Put it down to Friday afternoon!

    :-\

    Don't know what I was thinking that something as high profile as that
    could slip through the net!!
     
    Ant, Oct 6, 2006
    #6
  7. Ant

    Ant Guest

    John Machin wrote:

    > Now quick kill your post before the effbot spots it :)


    Too late - the post was 3 minutes ago you know ;-)
     
    Ant, Oct 6, 2006
    #7
  8. Ant wrote:
    > John Machin wrote:
    >
    >> Now quick kill your post before the effbot spots it :)

    >
    > Too late - the post was 3 minutes ago you know ;-)
    >

    Must be your lucky day - he did spot it, and you're still alive !-)


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Oct 6, 2006
    #8
  9. Ant wrote:
    > John Machin wrote:
    >
    >> Now quick kill your post before the effbot spots it :)

    >
    > Too late - the post was 3 minutes ago you know ;-)
    >

    +1 QOTW, BTW

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Oct 6, 2006
    #9
    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. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    587
  2. RedEye
    Replies:
    2
    Views:
    615
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    670
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    915
    tom_usenet
    Jun 27, 2003
  5. Maric Michaud
    Replies:
    0
    Views:
    7,221
    Maric Michaud
    Jun 24, 2006
Loading...

Share This Page