regex - Regular Expression to parse hostname from URL in C#? -


hey i'm curious how parse out host name in url using regular expressions in c#.

i have following regex:

regex regexurl = new regex("://(?<host>([a-z\\d][-a-z\\d]*[a-z\\d]\\.)*[a-z][-a-z\\d]+[a-z])"); 

but throws error when url not contain "http://", , not parse out "www." part of url.

so how code function parses out "hostname.com" url, if not contain "http://". :)

i wouldn't use regular expressions.

  1. convert 'http://' '' (empty string) in string - removes http:// if it's there
  2. split string on / array
  3. the hostname element @ index 0

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -