Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > re.sub: escaping capture group followed by numeric(s)

Reply
Thread Tools

re.sub: escaping capture group followed by numeric(s)

 
 
Jon Clements
Guest
Posts: n/a
 
      09-17-2010
Hi All,

(I reckon this is probably a question for MRAB and is not really
Python specific, but anyhow...)

Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1')

I've been searching around and I'm sure it'll be obvious when it's
pointed out, but how do I use the above to replace 1 with 11?
Obviously I can't use r'\11' because there is no group 11. I know I
can use a function to do it, but it seems to me there must be a way
without. Can I escape r'\11' somehow so that it's group 1 with a '1'
after it (not group 11).

Cheers,

Jon.

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-17-2010
On 17/09/2010 19:21, Jon Clements wrote:
> Hi All,
>
> (I reckon this is probably a question for MRAB and is not really
> Python specific, but anyhow...)
>
> Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1')
>
> I've been searching around and I'm sure it'll be obvious when it's
> pointed out, but how do I use the above to replace 1 with 11?
> Obviously I can't use r'\11' because there is no group 11. I know I
> can use a function to do it, but it seems to me there must be a way
> without. Can I escape r'\11' somehow so that it's group 1 with a '1'
> after it (not group 11).
>

re.sub(r'(\d+)', r'\g<1>', 'string1')
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-17-2010
Jon Clements wrote:

> (I reckon this is probably a question for MRAB and is not really
> Python specific, but anyhow...)
>
> Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1')
>
> I've been searching around and I'm sure it'll be obvious when it's
> pointed out, but how do I use the above to replace 1 with 11?
> Obviously I can't use r'\11' because there is no group 11. I know I
> can use a function to do it, but it seems to me there must be a way
> without. Can I escape r'\11' somehow so that it's group 1 with a '1'
> after it (not group 11).


Quoting

http://docs.python.org/library/re.html#re.sub

"""
In addition to character escapes and backreferences as described above,
\g<name> will use the substring matched by the group named name, as defined
by the (?P<name>...) syntax. \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.
"""

Peter
 
Reply With Quote
 
Jon Clements
Guest
Posts: n/a
 
      09-17-2010
On 17 Sep, 19:59, Peter Otten <(E-Mail Removed)> wrote:
> Jon Clements wrote:
> > (I reckon this is probably a question for MRAB and is not really
> > Python specific, but anyhow...)

>
> > Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1')

>
> > I've been searching around and I'm sure it'll be obvious when it's
> > pointed out, but how do I use the above to replace 1 with 11?
> > Obviously I can't use r'\11' because there is no group 11. I know I
> > can use a function to do it, but it seems to me there must be a way
> > without. Can I escape r'\11' somehow so that it's group 1 with a '1'
> > after it (not group 11).

>
> Quoting
>
> http://docs.python.org/library/re.html#re.sub
>
> """
> In addition to character escapes and backreferences as described above,
> \g<name> will use the substring matched by the group named name, as defined
> by the (?P<name>...) syntax. \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..
> """
>
> Peter


Thanks Peter and MRAB. I must have been through the docs half a dozen
times and missed that - what a muppet! One of those days I guess...

Cheers,

Jon.
 
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
Stylesheets are not followed after migration =?Utf-8?B?QW5kcmU=?= ASP .Net 6 04-13-2006 08:28 PM
Compiler warning: Non-exernalized string literal; it should be followed by //$NON-NLS-<n> Rhino Java 14 12-14-2005 08:55 AM
org.kxml.parser.XmlParser$DefaultParserException: Attribute name ressmust be followed by '='! @8:1764 Fahad Java 0 11-16-2005 02:52 PM
should query string followed by a file name? jrefactors@hotmail.com Java 1 08-12-2005 10:48 PM
REGEX for CUSIP followed by two numbers. Brent ASP .Net 1 07-28-2005 01:52 AM



Advertisments