re.group vs re.groups
Ignacio Vazquez-Abrams
ignacio at openservices.net
Wed Oct 10 20:19:46 EDT 2001
On Thu, 11 Oct 2001, Huaiyu Zhu wrote:
> Hi, I'm puzzled by the behavior of the group and groups attributes of the re
> match objects. Why would b.group(0) below return the entire match string?
>
> Python 2.1 (#1, Oct 2 2001, 15:35:43)
> [GCC 2.95.2 19991024 (release)] on linux2
> >>> import re
> >>> start = re.compile("^\[(\d{4})\] ")
> >>> b = start.match ("[0256] ")
> >>> b
> <SRE_Match object at 0x80d8658>
> >>> b.group()
> '[0256] '
> >>> b.group(0)
> '[0256] '
> >>> b.groups(0)
> ('0256',)
> >>> b.groups()
> ('0256',)
> >>> b.groups()[0]
> '0256'
Because that's what it does. Subgroup matches start at 1. <match>.groups() has
subgroup 1 as element 0, subgroup 2 as element 1, etc.
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
More information about the Python-list
mailing list