Created
March 22, 2013 05:55
-
-
Save troyscott/5219283 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Text.RegularExpressions; | |
| namespace UrlParser | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| string[] text = new string[25]; | |
| string regexp; | |
| string hostname = "(not set)"; | |
| string pagepath = "(not set)"; | |
| text[10] = "http://www5.mysite.com/mypage/index.php"; | |
| text[2] = "http://www5.mysite.com/mypage/index.php"; | |
| text[8] = "http://www5.mysite.com/mypage/"; | |
| text[7] = "//www.mysite.com/"; | |
| text[6] = "//www.mysite.com/test/index.php"; | |
| text[0] = "/mypage/"; | |
| bool startsWithHttp = Regex.IsMatch(text[0], @"^http[s]{0,1}://"); | |
| bool startsWithDoubleSlash = Regex.IsMatch(text[0], @"^//"); | |
| bool startsWithSingleSlash = Regex.IsMatch(text[0], @"^[/]{1}"); | |
| if (startsWithHttp) | |
| { | |
| Console.WriteLine("Starts with http"); | |
| Console.WriteLine(text[0]); | |
| regexp = @"^http[s]{0,1}://[w]{0,3}[0-9]{0,1}\.([\w]+\.[A-Za-z]{1,3})(/.+)$"; | |
| Match m = Regex.Match(text[0], regexp, RegexOptions.CultureInvariant); | |
| hostname = m.Groups[1].Value; | |
| pagepath = m.Groups[2].Value; | |
| } | |
| if (startsWithDoubleSlash) | |
| { | |
| Console.WriteLine("Starts with double slash"); | |
| Console.WriteLine(text[0]); | |
| regexp = @"^//[w]{0,3}[0-9]{0,1}\.([\w]+\.[A-Za-z]{1,3})((/.+)|(/))"; | |
| Match m = Regex.Match(text[0], regexp, RegexOptions.CultureInvariant); | |
| hostname = m.Groups[1].Value; | |
| pagepath = m.Groups[2].Value; | |
| } | |
| if (startsWithSingleSlash) | |
| { | |
| Console.WriteLine("Starts with single slash"); | |
| Console.WriteLine(text[0]); | |
| regexp = @"^(/.+)$"; | |
| Match m = Regex.Match(text[0], regexp, RegexOptions.CultureInvariant); | |
| hostname = "- Invalid -"; | |
| pagepath = m.Groups[1].Value; | |
| } | |
| Console.WriteLine("Hostname: {0}", hostname); | |
| Console.WriteLine("Page Path: {0}", pagepath); | |
| System.Console.Read(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment