[Tutor] expand
Kent Johnson
kent_johnson at skillsoft.com
Tue Aug 10 18:08:17 CEST 2004
match.expand(template) returns the string obtained by doing backslash
substitution on the template. The backslash values will be replaced with
the substring matched by the corresponding group of the regular expression.
Groups are defined by parentheses in the regex. For example \1 will be
substituted with the first group matched by the regex. In your example,
since the template only has the \xx value, it is the same as match.group(xx).
For example:
>>> import re
>>> r=re.compile(r'(a+).*?(b+)')
>>> m=r.search('aaaxxbbbb')
>>> m.expand('\\1')
'aaa'
>>> m.expand('\\2')
'bbbb'
>>> m.expand('\\1yy\\2')
'aaayybbbb'
>>> m.group(1)
'aaa'
>>> m.group(2)
'bbbb'
>>>
See http://docs.python.org/lib/match-objects.html for more information
about match objects
Kent
At 08:40 AM 8/10/2004 -0700, kumar s wrote:
>Dear group,
>
>I am looking into a code by a friend of mine.
>
>x = re.comile("XXXX")
>x_match = x.search(string)
>y = int(x_match.expand("\\3"))
>y1 = int(x_match.expand("\\5"))
>
>I did not understand what .expand is doing here. What
>is it doing. is it same as expandtabs method in string
>module?
>Can any one help me please.
>thank you
>
>SK
>
>
>
>__________________________________
>Do you Yahoo!?
>Read only the mail you want - Yahoo! Mail SpamGuard.
>http://promotions.yahoo.com/new_mail
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list