[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')
 >>> m.expand('\\2')
 >>> m.expand('\\1yy\\2')
 >>> m.group(1)
 >>> m.group(2)

See http://docs.python.org/lib/match-objects.html for more information 
about match objects


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
>Can any one help me please.
>thank you
>Do you Yahoo!?
>Read only the mail you want - Yahoo! Mail SpamGuard.
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list