# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:^(\w{1,255}):(.{1,255})@|^)(?:(?:(?=\S{0,253}(?:$|:| ))((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z0-9]{1,63})))|localhost)(:\d{1,5})?"
test_str = ("Valid localhost addresses:\n"
"localhost\n"
"user:password@localhost:80\n"
"localhost:8080/path/to/page.html\n\n\n"
"Valid IPv4 addresses:\n"
"10.10.0.1 \n"
"192.168.0.1 \n"
"192.168.0.1:8888 \n"
"user:password@192.168.0.1:8888 \n"
"user:password@192.168.0.1\n\n\n"
"Valid IPv6 addresses: // regex does not check for that, check here: http://vernon.mauery.com/content/projects/linux/ipv6_regex\n\n"
"100::\n"
"100::ffff:ffff:ffff:ffff\n"
"::ffff:0.0.0.0\n"
"64:ff9b::0.0.0.0\n"
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
"fe80::\n"
"2001:2f:ffff:ffff:ffff:ffff:ffff:ffff\n"
"2001:db8::\n"
"::1\n\n\n"
"Valid domains: // all should work\n"
"www.test.com\n"
"www1.test.com\n"
"www1.test.com:8080 \n"
"test.com\n"
"test-test.com\n"
"test.com/path/to/page?var=1&var2=2\n"
"test.com:80\n"
"user:password@test.com\n"
"user:password@test-test.com\n"
"user:password@test.com/path/to/page\n"
"user:password@test.com/path/to/page?var=1&var2=2\n"
"user:password@test.com:80\n"
"user:password@test.com:80/path/to/page?var=1&var2=2\n"
"Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.com\n"
"user:password@Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.com\n"
"user:password@Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.com:8080\n"
"Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.MaximumLengthOf63CharactersForATopLevelDomainWithoutTheDashes\n"
"Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.MaximumLengthOf63CharactersForATopLevelDomainWithoutTheDashes:8888\n"
"user:password@Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.Maximum-Length-Of-63-Characters-For-A-Subdomain-Completely-Used.MaximumLengthOf63CharactersForATopLevelDomainWithoutTheDashes:8888\n"
"a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a // Maximum number of 127 suddomains, seperated by 126 dots\n\n\n"
"Invalid domains: // should not work\n"
"justLongGibberishBLABLABLA \n"
"againButThisTimeWithNumbers12345675678797 \n"
"ShouldNOTWorkBecauseOfTheDash-.com\n"
"UrlLenghtIsValidButThisSubdomainIsTooLongMaximumForASubdomainIs63Characters.com \n"
"This-Url-Has-Valid-Subdomains.But-The-Url-Length-In-General-Is-Too-Long.This-Means-Longer-Than-253-Characters-For-The-Complete-Url.Including-The-Top-Level-Domain-Itself.So-This-Line-Shouldnt-Be-Catched-By-The-Regex.Its-Exactly-One-Character-Too-Long1.com")
matches = re.finditer(regex, test_str, re.MULTILINE | 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