Bug in re module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ant

    #1

    Bug in re module?

    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

    #2
    Re: Bug in re module?


    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.

    Comment

    • Just

      #3
      Re: Bug in re module?

      In article <1160150048.211 740.175620@k70g 2000cwa.googleg roups.com>,
      "Ant" <antroy@gmail.c omwrote:
      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

      Comment

      • John Machin

        #4
        Re: Bug in re module?


        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.

        Comment

        • Fredrik Lundh

          #5
          Re: Bug in re module?

          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>

          Comment

          • Ant

            #6
            Re: Bug in re module?


            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!!

            Comment

            • Ant

              #7
              Re: Bug in re module?


              John Machin wrote:
              Now quick kill your post before the effbot spots it :-)
              Too late - the post was 3 minutes ago you know ;-)

              Comment

              • Bruno Desthuilliers

                #8
                Re: Bug in re module?

                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 'onurb@xiludom. gro'.split('@')])"

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Bug in re module?

                  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 'onurb@xiludom. gro'.split('@')])"

                  Comment

                  Working...