Algorithm for finding and replacing URLs with hyperlinks

Peter Hansen peter at engcorp.com
Wed Feb 19 15:14:09 EST 2003


"Morten W. Petersen" wrote:
> 
> Does anyone have a snippet of code that'll replace for example
> http://www.python.org with <a href="http://www.python.org"
>  >http://www.python.org</a>?

How perfect do you want it?  Various packages include regular
expression patterns which match URLs, although some are better
than others.  The one in an older version of ZWiki which I 
checked, for example, looks like this:

 urlchars         = r'[A-Za-z0-9/:@_%~#=&\.\-\?]+'
 url              = r'["=]?((http|ftp|mailto|file):%s)' % (urlchars)

...but unfortunately that means it will be confused if you try
to put the URL at the end of a sentence like this http://www.python.org.

(The final period would confuse it.)

If you have the "right" pattern, using re.sub() is clearly the
most straightforward approach to doing the replacement.

-Peter




More information about the Python-list mailing list