Skip to content

Instantly share code, notes, and snippets.

@jordangray
Created May 28, 2014 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordangray/8af3d596d242507d7b61 to your computer and use it in GitHub Desktop.
Save jordangray/8af3d596d242507d7b61 to your computer and use it in GitHub Desktop.
Very basic URL parsing with regular expressions and named capture groups.
Regex ParseUrl = new Regex(@"(?:^\w*:?//)? # Protocol
(?<domain> [\.\w]+) # Domain
(?::\d)? # Port
(?:/.*)? # Path",
RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace);
// Example usage
var domain = ParseUrl.Replace(url, @"${domain}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment