0

Referring to http://docs.python.org/howto/regex.html section Non-capturing and named groups, I saw an example whose output is not obvious to me.

  >>> import re

  >>> m = re.match(r"([abc])+", "abc")
  >>> m.groups()

 ('c',)

Here I am not able to understand why the group(1) is 'c' and also why I see I dangling comma at the end. Can somebody help?

4

1 回答 1

2

I don't know about the dangling comma, but your first group is c, because you let the group repeat itself by putting the + after the group, and not after the character group.

This way, the regex first matches a, which is assigned to group 1. Then, it matches b, which is also assigned to group 1. And at last, it matches c, assigned to group 1 and finishes; thus leaving your group 1 to be c.

If you write ([abc]+), your group 1 will be abc.

于 2012-08-27T11:51:27.400 回答