Search code examples
pythonregexobjectmatchcapture-group

Why won't re.groups() give me anything for my one correctly-matched group?


When I run this code:

print re.search(r'1', '1').groups() 

I get a result of (). However, .group(0) gives me the match.

Shouldn't groups() give me something containing the match?


Solution

  • 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

    Edited.