Re: small problem with re.sub




"Astan Chee" <stanc@xxxxxxxxx> wrote in message news:mailman.78.1201748496.9267.python-list@xxxxxxxxxxxxx
Hi,
I have a html text stored as a string. Now I want to go through this string and find all 6 digit numbers and make links from them.
Im using re.sub and for some reason its not picking up the previously matched condition. Am I doing something wrong? This is what my code looks like:
htmlStr = re.sub('(?P<id>\d{6})','<a href=\"http://linky.com/(?P=id).html\">(?P=id)</a>',htmlStr)
It seems that it replaces it alright, but it replaces it literally. Am I not escaping certain characters?
Thanks again for the help.
Cheers

Animal Logic
http://www.animallogic.com

Please think of the environment before printing this email.

This email and any attachments may be confidential and/or privileged. If you are not the intended recipient of this email, you must not disclose or use the information contained in it. Please notify the sender immediately and delete this document if you have received it in error. We do not guarantee this email is error or virus free.



See the help for re.sub: "Backreferences, such as "\6", are replaced with the substring matched by group 6 in the pattern."

This should work:

htmlStr = re.sub('(?P<id>\d{6})','<a href="http://linky.com/\\1.html";>\\1</a>',htmlStr)

--Mark

.



Relevant Pages

  • Re: writing get_script as an external routine callable by C
    ... I _STRONGLY_ suggest to attend a Perl class or a self-study course where ... If you mean "begins with a digit" as you said above ... Case 2 matches if the beginning of the string is followed by 1 or more ... If you put the two Bushs together in their over seven years of their two ...
    (comp.lang.perl.misc)
  • Re: Sorted Fixed Length String
    ... >I have a String of Numerical Digits Created Using Concatenate. ... >The Strings could be from 6 Characters in Length to 11 Characters in ... >The Least Characters in a String with a Digit GREATER than 1 can ONLY be ... Dim str As String ...
    (microsoft.public.excel.programming)
  • Re: Pi as the Mother Number
    ... For example if we find our 100-digit string starting at the ... 37th digit of pi, we can just say, go to the 37th digit of pi and print the ... problem with using this principle to compress numbers is the problem of the ... Foundation of Mathematics, first to understand the difference between ...
    (sci.math)
  • Re: writing get_script as an external routine callable by C
    ... I _STRONGLY_ suggest to attend a Perl class or a self-study course where ... If you mean "begins with a digit" as you said above ... used as delimiters, therefore it doesn't show up in the FAQ. ... Case 2 matches if the beginning of the string is followed by 1 or more ...
    (comp.lang.perl.misc)
  • Re: a challenge
    ... // if the remainder isn't zero. ... // recursively to convert it to a string ... // of the hundred's digit (there can be only ...
    (alt.lang.asm)