# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<FULLREQUEST>^(?<HOST>(?<PROTOCOL>(?:http)?s?:?\/\/)(?<DOMAIN>[^\/]+\/)|(?<RELATIVE>(?:\.*\/?)+))(?<PATH>[^\s]+\/)?(?<FILENAME>(?:(?<NAME>[^\/\s\.\?\#]+?)?(?<EXT>\.[^\s\?\#]*)?|(?:\/[^\/\s\?\#]+?)))(?<QUERYSTRING>\?[^\s\#]+)?(?<ANCHOR>\#\S+)?$)"
test_str = ("Domain Name Root\n"
"https://www.domain.com/\n"
"https://www.domain.com/?with=queryString\n"
"https://www.domain.com/#withAnchor\n"
"https://www.domain.com/?with=queryString#andAnchor\n\n"
"Full Domain Name With File\n"
"https://www.domain.com/with-file\n"
"https://www.domain.com/with-file?with=queryString\n"
"https://www.domain.com/with-file#withAnchor\n"
"https://www.domain.com/with-file?with=queryString#andAnchor\n\n"
"Full Domain Name With File With Extension\n"
"https://www.domain.com/file-with.ext\n"
"https://www.domain.com/file-with.ext?with=queryString\n"
"https://www.domain.com/file-with.ext#withAnchor\n"
"https://www.domain.com/file-with.ext?with=queryString#andAnchor\n\n"
"Full Domain Name With Path to File\n"
"https://www.domain.com/some-path/to/a/file\n"
"https://www.domain.com/some-path/to/a/file?with=queryString\n"
"https://www.domain.com/some-path/to/a/file#withAnchor\n"
"https://www.domain.com/some-path/to/a/file?with=queryString#andAnchor\n\n"
"Full Domain Name With Path to File With Extension\n"
"https://www.domain.com/some-path/to/a/file-with.ext\n"
"https://www.domain.com/some-path/to/a/file-with.ext?with=queryString\n"
"https://www.domain.com/some-path/to/a/file-with.ext\n"
"https://www.domain.com/some-path/to/a/file-with.ext?with=queryString#andAnchor\n\n"
"Dynamic Protocol Full Domain\n"
"//www.domain.com/\n"
"//www.domain.com/?with=queryString\n"
"//www.domain.com/#withAnchor\n"
"//www.domain.com/?with=queryString#andAnchor\n\n"
"Dynamic Protocol (Full Domain) to File\n"
"//www.domain.com/with-file\n"
"//www.domain.com/with-file?with=queryString\n"
"//www.domain.com/with-file#withAnchor\n"
"//www.domain.com/with-file?with=queryString#andAnchor\n\n"
"Dynamic Protocol (Full Domain) to File With Extension\n"
"//www.domain.com/with-file-with.ext\n"
"//www.domain.com/with-file-with.ext?with=queryString\n"
"//www.domain.com/with-file-with.ext#withAnchor\n"
"//www.domain.com/with-file-with.ext?with=queryString#andAnchor\n\n"
"Dynamic Protocol (Full Domain), Path Leading to File\n"
"//www.domain.com/with/path/to/file\n"
"//www.domain.com/with/path/to/file?with=queryString\n"
"//www.domain.com/with/path/to/file#withAnchor\n"
"//www.domain.com/with/path/to/file?with=queryString#andAnchor\n\n"
"Dynamic Protocol (Full Domain), Path Leading to File With Extension\n"
"//www.domain.com/with/path/to/file-with.ext\n"
"//www.domain.com/with/path/to/file-with.ext?with=queryString\n"
"//www.domain.com/with/path/to/file-with.ext#withAnchor\n"
"//www.domain.com/with/path/to/file-with.ext?with=queryString#andAnchor\n"
"Root Relative File\n"
"/some-root-file\n"
"/some-root-file?with=queryString\n"
"/some-root-file#withAnchor\n"
"/some-root-file?with=queryString#andAnchor\n\n"
"Root Relative File With Extension\n"
"/root-file-with.ext\n"
"/root-file-with.ext?with=queryString\n"
"/root-file-with.ext#withAnchor\n"
"/root-file-with.ext?with=queryString#andAnchor\n\n"
"Root Path to File\n"
"/some-root-path/to/a/file\n"
"/some-root-path/to/a/file?with=queryString\n"
"/some-root-path/to/a/file#withAnchor\n"
"/some-root-path/to/a/file?with=queryString#andAnchor\n\n"
"Root Path to File With Extension\n"
"/some-root-path/to/a/file-with.ext\n"
"/some-root-path/to/a/file-with.ext?with=queryString\n"
"/some-root-path/to/a/file-with.ext#withAnchor\n"
"/some-root-path/to/a/file-with.ext?with=queryString#andAnchor\n\n"
"Relative (Same As Requestor) File\n"
"./relative-to-requester-file\n"
"./relative-to-requester-file?with=queryString\n"
"./relative-to-requester-file#withAnchor\n"
"./relative-to-requester-file?with=queryString#andAnchor\n\n"
"Relative (Same As Requestor) File With Extension\n"
"./relative-to-requester-file-with.ext\n"
"./relative-to-requester-file-with.ext?with=queryString\n"
"./relative-to-requester-file-with.ext#withAnchor\n"
"./relative-to-requester-file-with.ext?with=queryString#andAnchor\n\n"
"Relative (Same As Requestor) Path To File\n"
"./relative-to-requester-path/to/a/file\n"
"./relative-to-requester-path/to/a/file?with=queryString\n"
"./relative-to-requester-path/to/a/file#withAnchor\n"
"./relative-to-requester-path/to/a/file?with=queryString#andAnchor\n\n"
"Relative (Same As Requestor) Path To File With Extension\n"
"./relative-to-requester-path/to/a/file-with.ext\n"
"./relative-to-requester-path/to/a/file-with.ext?with=queryString\n"
"./relative-to-requester-path/to/a/file-with.ext#withAnchor\n"
"./relative-to-requester-path/to/a/file-with.ext?with=queryString#andAnchor\n\n"
"Relative (Above Requestor) File\n"
"../relative-file-up\n"
"../relative-file-up?with=queryString\n"
"../relative-file-up#withAnchor\n"
"../relative-file-up?with=queryString#andAnchor\n\n"
"Relative (Above Requestor) File With Extension\n"
"../relative-file-up-with.ext\n"
"../relative-file-up-with.ext?with=queryString\n"
"../relative-file-up-with.ext#withAnchor\n"
"../relative-file-up-with.ext?with=queryString#andAnchor\n\n"
"Relative (two Above Requestor) File\n"
"../../relative-file-two-up\n"
"../../relative-file-two-up?with=queryString\n"
"../../relative-file-two-up#withAnchor\n"
"../../relative-file-two-up?with=queryString#andAnchor\n\n"
"Relative (two Above Requestor) File With Extension\n"
"../../relative-file-two-up.ext\n"
"../../relative-file-two-up.ext?with=queryString\n"
"../../relative-file-two-up.ext#withAnchor\n"
"../../relative-file-two-up.ext?with=queryString#andAnchor\n\n"
"Relative (n Above Requestor) File\n"
"../../../../../../../../relative-file-n-up\n"
"../../../../../../../../relative-file-n-up?with=queryString\n"
"../../../../../../../../relative-file-n-up#withAnchor\n"
"../../../../../../../../relative-file-n-up?with=queryString#andAnchor\n\n"
"Relative Path (Above Requestor) to File\n"
"../relative-path/up/to/a/file\n"
"../relative-path/up/to/a/file?with=queryString\n"
"../relative-path/up/to/a/file#withAnchor\n"
"../relative-path/up/to/a/file?with=queryString#andAnchor\n\n"
"Relative Path (Above Requestor) to File With Extension\n"
"../relative-path/up/to/a/file-with.ext\n"
"../relative-path/up/to/a/file-with.ext?with=queryString\n"
"../relative-path/up/to/a/file-with.ext#withAnchor\n"
"../relative-path/up/to/a/file-with.ext?with=queryString#andAnchor\n\n"
"Relative Path (Two Above Requestor) to File\n"
"../../relative-path/two/up/to/a/file\n"
"../../relative-path/two/up/to/a/file?with=queryString\n"
"../../relative-path/two/up/to/a/file#withAnchor\n"
"../../relative-path/two/up/to/a/file?with=queryString#andAnchor\n\n"
"Relative Path (Two Above Requestor) to File With Extension\n"
"../../relative-path/two/up/to/a/file-with.ext\n"
"../../relative-path/two/up/to/a/file-with.ext?with=queryString\n"
"../../relative-path/two/up/to/a/file-with.ext#withAnchor\n"
"../../relative-path/two/up/to/a/file-with.ext?with=queryString#andAnchor\n\n"
"Relative Path (n Above Requestor) to File\n"
"../../../../../relative-path/n/up/to/a/file\n"
"../../../../../relative-path/n/up/to/a/file?with=queryString\n"
"../../../../../relative-path/n/up/to/a/file#withAnchor\n"
"../../../../../relative-path/n/up/to/a/file?with=queryString#andAnchor\n\n"
"Relative Path (n Above Requestor) to File With Extension\n"
"../../../../../relative-path/n/up/to/a/file-with.ext\n"
"../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString\n"
"../../../../../relative-path/n/up/to/a/file-with.ext#withAnchor\n"
"../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString#andAnchor\n")
matches = re.finditer(regex, test_str, re.IGNORECASE | re.MULTILINE)
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