import re
regex = re.compile(r"((?:ht|f)tps?:\/\/(\w+\.\w{2}[\/\w-.]*))", flags=re.IGNORECASE)
test_str = ("http://matthiasweiss.at/asd/asd\n\n"
"ftp://hallo.welt\n\n"
"https://hallowelt.jpg\n\n"
"http://asdasd.aa/jojoji.jiji\n\n"
"mail@matthiasweiss.at\n\n"
"http://matthiasweiss.at\n\n\n\n\n\n\n")
subst = "<a href=\"$1\">$1</a>"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html