Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: small problem with re.sub
Gabriel Genellina
1/31/2008 3:50:00 AM
En Thu, 31 Jan 2008 01:01:30 -0200, Astan Chee <stanc@al.com.au> escribió:
> 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://...
(?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?
Two errors:
- use raw strings r"..." to write regular expressions (or quote every
backslash...)
- if you want to *substitute* a named group, the syntax is "\g<name>"; you
have used the syntax to *match* a named group.
re.sub(r'(?P<id>\d{6})',r'<a
href="
http://...
\g<id>.html">\g<id></a>',htmlStr)
In simple cases like this, may be easier to just use a number:
re.sub(r'(\d{6})',r'<a href="
http://...
\1.html">\1</a>',htmlStr)
--
Gabriel Genellina
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: small problem with re.sub
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password