On 08/12/2010 02:23, Yingjie Lan wrote:
Hi,
According to the doc, group(0) is the entire match.
m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
m.group(0) # The entire match 'Isaac Newton'
But if you do this:
import re
re.sub(r'(\d{3})(\d{3})', r'\0 to \1-\2', '757234')
'\x00 to 757-234'
where I expected
'757234 to 757-234'
Then I found that in python re '\0' is considered an octal number.
So, is there anyway to refer to the entire match by an escaped
notation?
Use \g<0>.
This notation works in the replacement template for both named and
numbered groups:
>>> re.sub(r'(\d{3})(\d{3})', r'\g<0> to \g<1>-\g<2>', '757234')
'757234 to 757-234'
>>> re.sub(r'(?P<first>\d{3})(?P<second>\d{3})', r'\g<0> to
\g<first>-\g<second>', '757234')
'757234 to 757-234'
--
http://mail.python.org/mailman/listinfo/python-list