import re
regex = re.compile(r"(?<!=\")\b((?:http|https|ftp):\/\/)((?:(?:\w)+\.)+(?:\w)+)(:\d+)?((?:\/[\w]+)*[.\w]*\/?)((?:[\?\w\=\_\-%+!$^*\/\\@#;:]|(?:&(?!gt;)|(?:['\"](?=[\w\"'&#,]))))*)")
test_str = ("<http://www.google.com.edu:8080/path/to/ting.this?this=that&that=this#21> \n"
"<http://www.google.com/path/to/thing/?this=that&that=this#21>\n"
"http://google.com\n"
"https://www.yy.sdf.yahoo.com.sdf.s.dsf.cat/\n"
"\"ftp://msn.com/path.thing\"\n"
"'http://askdeeves.com/sdf?asdf=\"sdf\"&cat=blue'\n"
"<http://ask.com#21> \n"
"href=\"http://askdeeves.com/sdf?asdf=\"sdf\"&cat=blue\"\n\n\n\n\n")
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