URL replacement in text
Ryan Tracey
ryant at thawte.com
Tue Jan 16 10:09:22 EST 2001
Thanks for the example Fredrik. Steve, thanks for the caution. Much
obliged.
Cheers,
Ryan
> "Fredrik Lundh" <fredrik at effbot.org> wrote in message
> news:MrG76.5696$Qb7.794378 at newsb.telia.net...
> > Ryan Tracey wrote:
> > > Can anybody point me in the right direction on this one. I'd like to
> > > pass a block of plain text (string) to a function and have that function
> > > look for URLs and turn them into HTML URL tags
> >
> > import re
> >
> > links = re.compile("(?:http|ftp|gopher):[a-z0-9._~/%-]+")
> >
> > def fixlink(m):
> > href = m.group(0)
> > return "<a href='%s'>%s</a>" % (href, href)
> >
> > sometext = """
> > here's a link: http://www.pythonware.com
> > """
> >
> > print links.sub(fixlink, sometext)
> >
> > Hope this helps!
> >
> > Cheers /F
> >
> Tracey:
>
> 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]).
>
> Since it's about six times better that anything I could do, however, let
> this act as a caution rather than a correction.
>
> i-like-to-look-at-examples-too-but-avoid-re-when-I-can-ly y'rs - steve
--
Ryan Tracey
Thawte Certification
More information about the Python-list
mailing list