Algorithm for finding and replacing URLs with hyperlinks

Morten W. Petersen morten at nidelven-it.no
Wed Feb 19 16:00:19 EST 2003


Peter Hansen wrote:
> "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.

OK, I managed to make a function for replacing URLs with hyperlinks,
see attached code.  Thanks!  :)

-Morten


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: utilities.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20030219/c4683a47/attachment.ksh>


More information about the Python-list mailing list