re.group vs re.groups

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Oct 10 20:08:45 EDT 2001


Replying to my own post a few minutes ago ...

On Thu, 11 Oct 2001 00:01:06 +0000 (UTC), Huaiyu Zhu
<huaiyu at gauss.almadan.ibm.com> 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?

Never mind.  I found out that group(0) has this special meaning.  The actual
group index starts from one.  So b.group(1) does it.  Should have read the
doc more carefully ...

>
>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'
>
>Huaiyu
>



More information about the Python-list mailing list