468,303 Members | 1,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,303 developers. It's quick & easy.

Bug in re module?

Ant
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.

Oct 6 '06 #1
8 877
Ant

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.

Oct 6 '06 #2
In article <11**********************@k70g2000cwa.googlegroups .com>,
"Ant" <an****@gmail.comwrote:
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
Oct 6 '06 #3

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.
Oct 6 '06 #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>

Oct 6 '06 #5
Ant

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

Oct 6 '06 #6
Ant

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

Oct 6 '06 #7
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 'o****@xiludom.gro'.split('@')])"
Oct 6 '06 #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 ;-)
+1 QOTW, BTW

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Irmen de Jong | last post: by
5 posts views Thread by dody suria wijaya | last post: by
3 posts views Thread by David T. Ashley | last post: by
40 posts views Thread by rjcarr | last post: by
4 posts views Thread by rkmr.em | last post: by
reply views Thread by Fredrik Lundh | last post: by
6 posts views Thread by dudeja.rajat | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.