Regular Expression Non Capturing Grouping Does Not Work.

V

Virtual Buddha

Hello all,

I am having some difficulties with the non-capturing grouping in
python regular expression module.

Even the code from the online documentation (http://docs.python.org/
howto/regex.html#non-capturing-and-named-groups) does not seem to
work.

As per the docs given in the link above this should happen (text
copied from the page):
m = re.match("([abc])+", "abc")
m.groups() ('c',)
m = re.match("(?:[abc])+", "abc")
m.groups()
()

BUT, this is what I get:
m = re.match("([abc])+", "abc")
m.group() 'abc'
m = re.match("(?:[abc])+", "abc")
m.group()
'abc'

I am using python 2.6 on opensuse 11.1. Any one know what I might be
doing wrong? Or is this a bug?

Thank you
VB
 
P

Peter Otten

Virtual said:
Hello all,

I am having some difficulties with the non-capturing grouping in
python regular expression module.

Even the code from the online documentation (http://docs.python.org/
howto/regex.html#non-capturing-and-named-groups) does not seem to
work.

As per the docs given in the link above this should happen (text
copied from the page):
m = re.match("([abc])+", "abc")
m.groups() ('c',)
m = re.match("(?:[abc])+", "abc")
m.groups()
()

BUT, this is what I get:
m = re.match("([abc])+", "abc")
m.group() 'abc'
m = re.match("(?:[abc])+", "abc")
m.group()
'abc'

I am using python 2.6 on opensuse 11.1. Any one know what I might be
doing wrong? Or is this a bug?

group != groups

match.group() or match.group(0) gives you a special group that comprises the
whole match. Regular capturing groups start at index 1, and only those are
returned by match.groups():
re.match("(?:[abc])+", "abc").group() # one group 'abc'
re.match("(?:[abc])+", "abc").groups() # all capturing groups
()

Peter
 
M

Miles Kaufmann

Hello all,

I am having some difficulties with the non-capturing grouping in
python regular expression module.

Even the code from the online documentation (http://docs.python.org/
howto/regex.html#non-capturing-and-named-groups) does not seem to
work.

...

Notice that you are calling .group() on the match object instead
of .groups(). Without any arguments, .group() is equivalent
to .group(0), which means "return the entire matching string."

http://docs.python.org/library/re.html#re.MatchObject.group

-Miles
 
V

Virtual Buddha

group != groups

match.group() or match.group(0) gives you a special group that comprises the
whole match. Regular capturing groups start at index 1, and only those are
returned by match.groups():
re.match("(?:[abc])+", "abc").group() # one group 'abc'
re.match("(?:[abc])+", "abc").groups() # all capturing groups

()

Peter


Aaargh! Should have caught that myself. Sorry for wasting all your
time. Thank you Peter and Miles! (I am off to bed now. 4:10 am :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top