import re

urlchars = r'[A-Za-z0-9/:@_%~#=&\.\-\?]+'
url = r'["=]?((http|ftp|https):%s)' % urlchars
example = """At http://www.python.org you can see things.
At ftp://ftp.python.org you can download things.
At https://www.python.org you can't find anything!"""

def hyperlink_urls(text):
    """Replaces all URLs in the text with HTML hyperlinks."""
    for protocol in protocols:
        protocol = re.compile(url, re.I|re.S)
        def replace(match):
            url = match.groups()[0]
            return '<a href="%s">%s</a>' % (url, url)
    text = protocol.subn(replace, text)[0]
    return text

if __name__ == '__main__':
    print hyperlink_urls(example)
