Export (0) Print
Expand All

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

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.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim url As String = "http://www.contoso.com:8080/letters/readme.html"  
      Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
                         RegexOptions.None, TimeSpan.FromMilliseconds(150))

      Dim m As Match = r.Match(url)
      If m.Success Then
      End If    
   End Sub 
End Module 
' 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)
© 2014 Microsoft