When I run this code:
print re.search(r'1', '1').groups()
I get a result of
.group(0) gives me the match.
groups() give me something containing the match?
Update: Thanks for the answers. So that means if I do
re.search() with no subgroups, I have to use
groups(0) to get a match?
groups is empty since you do not have any capturing groups - http://docs.python.org/library/re.html#re.MatchObject.groups. group(0) will always returns the whole text that was matched regardless of if it was captured in a group or not
To the best of my knowledge,
.groups() returns a tuple of remembered groups. I.e. those groups in the regular expression that are enclosed in parentheses. So if you were to write:
print re.search(r'(1)', '1').groups()
you would get
as your response. In general,
.groups() will return a tuple of all the groups of objects in the regular expression that are enclosed within parentheses.