URL replacement in text

Fredrik Lundh fredrik at effbot.org
Tue Jan 16 12:38:11 EST 2001


Steve Holden wrote:
> Note that this won't cope with some of the more pathological URLs (such as
> those with CGI arguments [http://system/cgi?arg1=val1] or those which link
> to a named anchor in the target page [http://system/pageref#target-name]).

import re

links = re.compile(r"(?i)(?:http|ftp|gopher):[\w.~/%#?&=-]+")

def fixlink(m):
    href = m.group(0)
    return "<a href='%s'>%s</a>" % (href, href)

sometext = """
here's another link: http://www.pythonware.com?FOO=1&bar=10#BAR
"""

print links.sub(fixlink, sometext)

Cheers /F





More information about the Python-list mailing list