import re
regex = re.compile(r"^(?<Scheme>[file|ftp|https{0,1}|ldap|telnet]+:\/{2})?(?<Subdomain>[a-zA-Z0-9-]{0,4}\.)?(?<Domain>\w+\.\w+\.?\w+\/)(?<Subdirectory>@[\w.]+\/)?(?<Path>[\w_~.-]+\/?[\w_-]+\/{0,1}?[\w_~.-]+)(?<Query>[%?&;].[\w&_~.=-]+)?(?<Fragment>#?[\/\w_-~.,=&-]+)?$", flags=re.MULTILINE)
test_str = ("https://medium.com/@s.vive00/7-changes-i-experienced-after-going-1-week-without-added-sugar-9bc48b667f87?source=email-8f87fbd013ca-1581664260785-digest.weekly------0-58------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top\n\n"
"https://medium.com/@DrAyala/exercise-is-the-answer-heres-why-it-s-a-weight-loss-underperformer-8b64103b8c8f?source=email-8f87fbd013ca-1581664260785-digest.weekly------1-59------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top#Fragment-identifier\n\n"
"https://www.medium.com/@DrAyala/exercise-is-the-answer-heres-why-it-s-a-weight-loss-underperformer-8b64103b8c8f?source=email-8f87fbd013ca-1581664260785-digest.weekly------1-59------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top#Fragment-identifier\n\n\n"
"http://www.yoursite.com/path/to/file.mp3\n\n"
"http://example.com/data.csv#row=4\n\n"
"http://example.com/bar.webm#t=40,80&xywh=160,120,320,240\n\n"
"example.com/data.csv#row=4\n\n"
"example.co.uk/data.csv#row=4")
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