replacing a part of a string using a regexp

Fredrik Lundh fredrik at pythonware.com
Thu Jul 18 17:11:15 EDT 2002


Rajarshi Guha wrote:

> 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?

(if you don't read the documentation, how come you always
find the right module/function to use? ;-)

you can use backreferences, or a callback method.  I prefer
callbacks (faster, more pythonic):

    def fixup(m):
        return "http://myserver.com/prog.py?" + m.group(1)

    page = re.sub('<a href="([^"]*)">', fixup, page)

also see:

    http://effbot.org/zone/re-sub.htm

</F>





More information about the Python-list mailing list