By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Bug in re module?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.