# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
(?#First, match the protocol)
(?:https?|ftp)://
(?#Next, check for optional username and/or password)
(?#Note: The following two char classes are functionally equivalent)
(?:[\x21-\x39\x3b-\x3f\x41-\x7e]+(?::[!-9;-?A-~]+)?@)?
(?#Next, let's match the domain [with support for Punycode ])
(?:xn--[0-9a-z]+|[0-9A-Za-z_-]+\.)*(?:xn--[0-9a-z]+|[0-9A-Za-z-]+)\.(?:xn--[0-9a-z]+|[0-9A-Za-z]{2,10})
(?#Let's match on optional port)
(?::(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{1,3}|\d))?
(?#Next, let's match on the path)
(?:/[\x21\x22\x24\x25\x27-x2e\x30-\x3b\x3e\x40-\x5b\x5d-\x7e]*)*
(?#Next, let's match on an anchor)
(?:\#[\x21\x22\x24\x25\x27-x2e\x30-\x3b\x3e\x40-\x5b\x5d-\x7e]*)?
(?#Last, but not least, we match on URI params)
(?:\?[\x21\x22\x24\x25\x27-\x2e\x30-\x3b\x40-\x5b\x5d-\x7e]+=[\x21\x22\x24\x25\x27-\x2e\x30-\x3b\x40-\x5b\x5d-\x7e]*)?
(?#Additional params)
(?:&[\x21\x22\x24\x25\x27-\x2e\x30-\x3b\x40-\x5b\x5d-\x7e]+=[\x21\x22\x24\x25\x27-\x2e\x30-\x3b\x40-\x5b\x5d-\x7e]*)*
"""
test_str = ("https://thechildrenareourfuture.org/#SomeRandomAnchorGoesHere\n"
"http://user:pass@example.com:8080/omega:33\n"
"https://www.google.com:65535/\n"
"https://www.google.com:42069/\n"
"https://www.google.com:9876/\n"
"https://www.google.com:59999/\n"
"https://www.google.com:10000/\n"
"https://www.google.com:1234/\n"
"https://www.google.com:53/\n"
"https://www.google.com:123/\n"
"https://www.google.com:0/\n"
"ftp://username:password@example.com:21/path/here/to/file.tar.gz\n"
"http://google.com?redirect=https%3A%2F%2Flocalhost%3A8080&var2=somethingelse\n"
"https://john.smith@github.com/UserGroup/repo.git\n"
"https://mattermost.com:8065/team/messages/@john.smith\n"
"https://punycode.xn--j6w193g\n\n"
"smtp://somesite.com/myFile.php\n"
"SMTP://SOMESITE.COM/MYFILE.PHP\n"
"unknown://somesite.com/fiLeNuM12345.php\n"
"//SomeSite.ru/foO.php\n"
"://SomeOtherSite.oRg/baRR.php\n\n"
"https://_sub1.sub2.sub3.s-u_b4.domain.com\n\n"
"http://username:password@subdomain.domain.co.uk/path/to/file.html#Anchor1?key1=value1&key2=value2")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE | re.IGNORECASE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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