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

Discussion in 'Python' started by Jon Clements, Sep 17, 2010.

  1. Jon Clements

    Jon Clements Guest

    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.
    Jon Clements, Sep 17, 2010
    #1
    1. Advertising

  2. Jon Clements

    MRAB Guest

    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')
    MRAB, Sep 17, 2010
    #2
    1. Advertising

  3. Jon Clements

    Peter Otten Guest

    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
    Peter Otten, Sep 17, 2010
    #3
  4. Jon Clements

    Jon Clements Guest

    On 17 Sep, 19:59, Peter Otten <> 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.
    Jon Clements, Sep 17, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    929
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    810
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    956
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Ben
    Replies:
    2
    Views:
    876
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,015
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page