use strict;
my $str = 'Domain Name Root
https://www.domain.com/
https://www.domain.com/?with=queryString
https://www.domain.com/#withAnchor
https://www.domain.com/?with=queryString#andAnchor
Full Domain Name With File
https://www.domain.com/with-file
https://www.domain.com/with-file?with=queryString
https://www.domain.com/with-file#withAnchor
https://www.domain.com/with-file?with=queryString#andAnchor
Full Domain Name With File With Extension
https://www.domain.com/file-with.ext
https://www.domain.com/file-with.ext?with=queryString
https://www.domain.com/file-with.ext#withAnchor
https://www.domain.com/file-with.ext?with=queryString#andAnchor
Full Domain Name With Path to File
https://www.domain.com/some-path/to/a/file
https://www.domain.com/some-path/to/a/file?with=queryString
https://www.domain.com/some-path/to/a/file#withAnchor
https://www.domain.com/some-path/to/a/file?with=queryString#andAnchor
Full Domain Name With Path to File With Extension
https://www.domain.com/some-path/to/a/file-with.ext
https://www.domain.com/some-path/to/a/file-with.ext?with=queryString
https://www.domain.com/some-path/to/a/file-with.ext
https://www.domain.com/some-path/to/a/file-with.ext?with=queryString#andAnchor
Dynamic Protocol Full Domain
//www.domain.com/
//www.domain.com/?with=queryString
//www.domain.com/#withAnchor
//www.domain.com/?with=queryString#andAnchor
Dynamic Protocol (Full Domain) to File
//www.domain.com/with-file
//www.domain.com/with-file?with=queryString
//www.domain.com/with-file#withAnchor
//www.domain.com/with-file?with=queryString#andAnchor
Dynamic Protocol (Full Domain) to File With Extension
//www.domain.com/with-file-with.ext
//www.domain.com/with-file-with.ext?with=queryString
//www.domain.com/with-file-with.ext#withAnchor
//www.domain.com/with-file-with.ext?with=queryString#andAnchor
Dynamic Protocol (Full Domain), Path Leading to File
//www.domain.com/with/path/to/file
//www.domain.com/with/path/to/file?with=queryString
//www.domain.com/with/path/to/file#withAnchor
//www.domain.com/with/path/to/file?with=queryString#andAnchor
Dynamic Protocol (Full Domain), Path Leading to File With Extension
//www.domain.com/with/path/to/file-with.ext
//www.domain.com/with/path/to/file-with.ext?with=queryString
//www.domain.com/with/path/to/file-with.ext#withAnchor
//www.domain.com/with/path/to/file-with.ext?with=queryString#andAnchor
Root Relative File
/some-root-file
/some-root-file?with=queryString
/some-root-file#withAnchor
/some-root-file?with=queryString#andAnchor
Root Relative File With Extension
/root-file-with.ext
/root-file-with.ext?with=queryString
/root-file-with.ext#withAnchor
/root-file-with.ext?with=queryString#andAnchor
Root Path to File
/some-root-path/to/a/file
/some-root-path/to/a/file?with=queryString
/some-root-path/to/a/file#withAnchor
/some-root-path/to/a/file?with=queryString#andAnchor
Root Path to File With Extension
/some-root-path/to/a/file-with.ext
/some-root-path/to/a/file-with.ext?with=queryString
/some-root-path/to/a/file-with.ext#withAnchor
/some-root-path/to/a/file-with.ext?with=queryString#andAnchor
Relative (Same As Requestor) File
./relative-to-requester-file
./relative-to-requester-file?with=queryString
./relative-to-requester-file#withAnchor
./relative-to-requester-file?with=queryString#andAnchor
Relative (Same As Requestor) File With Extension
./relative-to-requester-file-with.ext
./relative-to-requester-file-with.ext?with=queryString
./relative-to-requester-file-with.ext#withAnchor
./relative-to-requester-file-with.ext?with=queryString#andAnchor
Relative (Same As Requestor) Path To File
./relative-to-requester-path/to/a/file
./relative-to-requester-path/to/a/file?with=queryString
./relative-to-requester-path/to/a/file#withAnchor
./relative-to-requester-path/to/a/file?with=queryString#andAnchor
Relative (Same As Requestor) Path To File With Extension
./relative-to-requester-path/to/a/file-with.ext
./relative-to-requester-path/to/a/file-with.ext?with=queryString
./relative-to-requester-path/to/a/file-with.ext#withAnchor
./relative-to-requester-path/to/a/file-with.ext?with=queryString#andAnchor
Relative (Above Requestor) File
../relative-file-up
../relative-file-up?with=queryString
../relative-file-up#withAnchor
../relative-file-up?with=queryString#andAnchor
Relative (Above Requestor) File With Extension
../relative-file-up-with.ext
../relative-file-up-with.ext?with=queryString
../relative-file-up-with.ext#withAnchor
../relative-file-up-with.ext?with=queryString#andAnchor
Relative (two Above Requestor) File
../../relative-file-two-up
../../relative-file-two-up?with=queryString
../../relative-file-two-up#withAnchor
../../relative-file-two-up?with=queryString#andAnchor
Relative (two Above Requestor) File With Extension
../../relative-file-two-up.ext
../../relative-file-two-up.ext?with=queryString
../../relative-file-two-up.ext#withAnchor
../../relative-file-two-up.ext?with=queryString#andAnchor
Relative (n Above Requestor) File
../../../../../../../../relative-file-n-up
../../../../../../../../relative-file-n-up?with=queryString
../../../../../../../../relative-file-n-up#withAnchor
../../../../../../../../relative-file-n-up?with=queryString#andAnchor
Relative Path (Above Requestor) to File
../relative-path/up/to/a/file
../relative-path/up/to/a/file?with=queryString
../relative-path/up/to/a/file#withAnchor
../relative-path/up/to/a/file?with=queryString#andAnchor
Relative Path (Above Requestor) to File With Extension
../relative-path/up/to/a/file-with.ext
../relative-path/up/to/a/file-with.ext?with=queryString
../relative-path/up/to/a/file-with.ext#withAnchor
../relative-path/up/to/a/file-with.ext?with=queryString#andAnchor
Relative Path (Two Above Requestor) to File
../../relative-path/two/up/to/a/file
../../relative-path/two/up/to/a/file?with=queryString
../../relative-path/two/up/to/a/file#withAnchor
../../relative-path/two/up/to/a/file?with=queryString#andAnchor
Relative Path (Two Above Requestor) to File With Extension
../../relative-path/two/up/to/a/file-with.ext
../../relative-path/two/up/to/a/file-with.ext?with=queryString
../../relative-path/two/up/to/a/file-with.ext#withAnchor
../../relative-path/two/up/to/a/file-with.ext?with=queryString#andAnchor
Relative Path (n Above Requestor) to File
../../../../../relative-path/n/up/to/a/file
../../../../../relative-path/n/up/to/a/file?with=queryString
../../../../../relative-path/n/up/to/a/file#withAnchor
../../../../../relative-path/n/up/to/a/file?with=queryString#andAnchor
Relative Path (n Above Requestor) to File With Extension
../../../../../relative-path/n/up/to/a/file-with.ext
../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString
../../../../../relative-path/n/up/to/a/file-with.ext#withAnchor
../../../../../relative-path/n/up/to/a/file-with.ext?with=queryString#andAnchor
';
my $regex = qr/(?<FULLREQUEST>^(?<HOST>(?<PROTOCOL>(?:http)?s?:?\/\/)(?<DOMAIN>[^\/]+\/)|(?<RELATIVE>(?:\.*\/?)+))(?<PATH>[^\s]+\/)?(?<FILENAME>(?:(?<NAME>[^\/\s\.\?\#]+?)?(?<EXT>\.[^\s\?\#]*)?|(?:\/[^\/\s\?\#]+?)))(?<QUERYSTRING>\?[^\s\#]+)?(?<ANCHOR>\#\S+)?$)/imp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html