1 out of 2 rated this helpful - Rate this topic

How to: Extract a Protocol and Port Number from a URL 

The following code example uses Match.Result to extract a protocol and port number from a URL. For example, "http://www.contoso.com:8080/letters/readme.html" would return "http:8080".

Example

    String Extension(String url) 
    {
        Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
            RegexOptions.Compiled);
        return r.Match(url).Result("${proto}${port}"); 
    }

See Also

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.