import re
regex = re.compile(r"^(?:[a-z][0-9a-z-.+]*:\/\/)?((?:(?:[a-z\u00a1-\uffff0-9_]+-?)*[a-z\u00a1-\uffff0-9_]+)(?:\.(?:[a-z\u00a1-\uffff0-9_]+-?)*[a-z\u00a1-\uffff0-9_]+)*(?:\.(?:[a-z0-9\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/.*)?(?:\?.*)?(?:\#.*)?$", flags=re.MULTILINE | re.IGNORECASE)
test_str = ("socks://www.example.com\n"
"10.1.1.1\n"
"10.1.1.2/4\n"
"10.1.1.3/0\n"
"10.1.1.0/8\n"
"10.1.1.5/32\n"
"10.1.1.6/35\n"
"10.1.1.7/36000\n"
"10.1.1.8/abcd\n"
"10.1.1.9-10.1.1.10\n"
"user:pass@zxuz.com\n"
"user@proxy.brew.opendnstest.com/customBlockUserInfo\n"
"user:@proxy.brew1.opendnstest.com/customBlockUserInfo\n"
"user:password@proxy.brew2.opendnstest.com/customBlockUserInfo\n"
"ftp://cnn.example.com&story=breaking_news@foobar.com/top_story.htm\n"
"foo.com:8080/bar/baz\n"
"asdfasdf.com/foo/b%20ar\n"
"https://example.com/archive/*/http://somesite.com\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