replacing a part of a string using a regexp

Siggy Brentrup bsb at winnegan.de
Thu Jul 18 17:23:18 EDT 2002


Rajarshi Guha <rajarshi at presidency.com> writes:

> Hi,
>   I'm trying to parse some HTML tags and I have the following situation:
> 
> a line contains:
> 
> <a href="www.myurl.com">
> 
> And I want to replace the url and make the line:
> 
> <a href="http://myserver.com/prog.py?www.myurl.com">
> 
> Using re.sub I can substitute the whole string bewteen quotes, but the
> substituion needs to use the string that is *found* in the quotes.
> 
> How can I access the matched portion in a re.sub() expression?

How about (tested w/ python2.2.1)

>>> re.sub('="(?P<anchor>[^"]*)"',
...        '="http://myserver.com/prog.py?\g<anchor>"',
...        '<a href="www.myurl.com">')
'<a href="http://myserver.com/prog.py?www.myurl.com">'

HTH
  Siggy

-- 
Siggy Brentrup - bsb at winnegan.de - http://oss.winnegan.de/
****** ceterum censeo javascriptum esse restrictam *******





More information about the Python-list mailing list