This documentation is archived and is not being maintained.

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

Updated: December 2009

The following example extracts a protocol and port number from a URL.

The example uses the Match.Result method to return the protocol followed by a colon followed by the port number.

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string url = "";

      Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/");
      Match m = r.Match(url);
      if (m.Success)
// The example displays the following output: 
//       http:8080

The regular expression pattern ^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/ can be interpreted as shown in the following table.




Begin the match at the start of the string.


Match one or more word characters. Name this group proto.


Match a colon followed by two slash marks.


Match one or more occurrences (but as few as possible) of any character other than a slash mark.


Match zero or one occurrence of a colon followed by one or more digit characters. Name this group port.


Match a slash mark.

The Match.Result method expands the ${proto}${port} replacement sequence, which concatenates the value of the two named groups captured in the regular expression pattern. It is a convenient alternative to explicitly concatenating the strings retrieved from the collection object returned by the Match.Groups property.

The example uses the Match.Result method with two substitutions, ${proto} and ${port}, to include the captured groups in the output string. You can retrieve the captured groups from the match's GroupCollection object instead, as the following code shows.

Console.WriteLine(m.Groups["proto"].Value + m.Groups["port"].Value); 




December 2009

Expanded the Comments section.

Customer feedback.

July 2009

Revised extensively.

Customer feedback.