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.""" regexp = re.compile(url, re.I|re.S) def replace(match): url = match.groups()[0] return '%s' % (url, url) text = regexp.subn(replace, text)[0] return text if __name__ == '__main__': print hyperlink_urls(example)