Created
May 5, 2011 17:32
-
-
Save justincjahn/957475 to your computer and use it in GitHub Desktop.
URL Parsing Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
# Breakdown | |
# ([^?:]+):?([0-9]+)?\??([^#]+)?(#.*)? | |
# ([^?:]+) Matches the file path. | |
# :?([0-9]+)? Matches the port number (optional). | |
# \??([^#]+)? Matches the query string minus the ? (optional). | |
# (#.*)? Matches the anchor (optional). | |
# The URL that outlines all of the matches possible | |
my $url = "/this/is/a/test.php:8080?foo=bar&baz#heading-1"; | |
# Do the match to demonstrate output | |
if ($url =~ m/([^?:]+):?([0-9]+)?\??([^#]+)?(#.*)?/) { | |
print STDOUT "URL:\t\t$0\n"; | |
print STDOUT "Path:\t\t$1\n"; | |
print STDOUT "Port:\t\t$2\n"; | |
print STDOUT "Query:\t\t$3\n"; | |
print STDOUT "Anchor:\t\t$4\n"; | |
} | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment