Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Created May 5, 2011 17:32
Show Gist options
  • Save justincjahn/957475 to your computer and use it in GitHub Desktop.
Save justincjahn/957475 to your computer and use it in GitHub Desktop.
URL Parsing Regex
#!/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