URL replacement in text

Steve Holden sholden at holdenweb.com
Tue Jan 16 17:42:27 EST 2001


"Fredrik Lundh" <fredrik at effbot.org> wrote in message
news:7G%86.7011$AH6.1061749 at newsc.telia.net...
> 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
>
Fredrik:

What took you so long? :-) Of course the next refinement is to present the
base part of the URL (up to the "?" or "#") as the anchored text, but use
the full URL as the HREF attribute value.

if-you've-got-answers-i've-got-questions-ly y'rs  - steve





More information about the Python-list mailing list