import re
regex = re.compile(r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&\/=]*)", flags=re.MULTILINE)
test_str = ("http://foo.com/blah_blah\n"
"http://foo.com/blah_blah/\n"
"http://foo.com/blah_blah_(wikipedia)\n"
"http://www.example.com/wpstyle/?p=364\n"
"https://www.example.com/foo/?bar=baz&inga=42&quux\n"
"http://userid:password@example.com:8080\n"
"http://foo.com/blah_(wikipedia)#cite-1\n"
"www.google.com\n"
"http://../\n"
"http:// shouldfail.com\n"
"http://224.1.1.1\n"
"http://142.42.1.1:8080/\n"
"ftp://foo.bar/baz\n"
"http://1337.net\n"
"http://foo.bar/?q=Test%20URL-encoded%20stuff\n"
"http://code.google.com/events/#&product=browser\n"
"http://-error-.invalid/\n"
"http://3628126748\n"
"http://उदाहरण.परीक्षा")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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