Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > group 0 in the re module

Reply
Thread Tools

group 0 in the re module

 
 
Yingjie Lan
Guest
Posts: n/a
 
      12-08-2010
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?

Thanks,

Yingjie



 
Reply With Quote
 
 
 
 
J. Gerlach
Guest
Posts: n/a
 
      12-08-2010
Am 08.12.2010 03:23, schrieb Yingjie Lan:
> 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?
>
> Thanks,
>
> Yingjie
>

the documentation of the re module says:

> \g<number> uses the corresponding group number; \g<2> is
> therefore equivalent to \2, but isn’t ambiguous in a replacement such
> as \g<2>0. \20 would be interpreted as a reference to group 20, not a
> reference to group 2 followed by the literal character '0'. The
> backreference \g<0> substitutes in the entire substring matched by
> the RE.


... so you're looking for r"\g<0> to \1-\2"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Multiple isakmp policies (Group 1 and Group 2) rmcnutt Cisco 1 07-13-2004 09:33 PM
Re: Which group is the PC discussion group (like comp.sys.mac.----)? philo Computer Support 0 08-03-2003 11:41 AM
Re: Which group is the PC discussion group (like comp.sys.mac.----)? LooseLips Computer Support 0 08-03-2003 08:37 AM



Advertisments