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.
- convert 'http://' '' (empty string) in string - removes http:// if it's there
- split string on / array
- the hostname element @ index 0
Comments
Post a Comment