Bug in re module?
John Machin
sjmachin at lexicon.net
Fri Oct 6 11:58:31 EDT 2006
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.
More information about the Python-list
mailing list