C#idiomatic C#
how-to-extract-a-protocol-and-port-number-from-a-urlstandard/base-types

Core library

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

Learn more about: How to: Extract a Protocol and Port Number from a URL

  • page standard/base-types/how-to-extract-a-protocol-and-port-number-from-a-url
  • updated 06/30/2020

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

Example

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

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/RegularExpressions.Examples.Protocol/cs/Example.cs1

csharp
// <Snippet1>
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string url = "http://www.contoso.com:8080/letters/readme.html";

      Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
                          RegexOptions.None, TimeSpan.FromMilliseconds(150));
      Match m = r.Match(url);
      if (m.Success)
         Console.WriteLine(m.Result("${proto}${port}"));
   }
}
// The example displays the following output:
//       http:8080
// </Snippet1>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/RegularExpressions.Examples.Protocol/vb/Example.vb1

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet1>
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
            Console.WriteLine(m.Result("${proto}${port}"))
        End If
    End Sub
End Module
' The example displays the following output:
'       http:8080
' </Snippet1>

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

Pattern Description
^ Begin the match at the start of the string.
(?<proto>\w+) 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.
(?<port>:\d+)? Match zero or one occurrence of a colon followed by one or more digit characters. Name this group port.
/ Match a slash mark.

The System.Text.RegularExpressions.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 System.Text.RegularExpressions.Match.Groups property.

The example uses the System.Text.RegularExpressions.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 System.Text.RegularExpressions.GroupCollection object instead, as the following code shows.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/RegularExpressions.Examples.Protocol/cs/example2.cs2

csharp
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string url = "http://www.contoso.com:8080/letters/readme.html";

      Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
                          RegexOptions.None, TimeSpan.FromMilliseconds(150));
      Match m = r.Match(url);
      if (m.Success)
// <Snippet2>
         Console.WriteLine(m.Groups["proto"].Value + m.Groups["port"].Value);
// </Snippet2>
   }
}
// The example displays the following output:
//       http:8080

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/RegularExpressions.Examples.Protocol/vb/example2.vb2

vb
' Visual Basic .NET Document
Option Strict On

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
            ' <Snippet2>
            Console.WriteLine(m.Groups("proto").Value + m.Groups("port").Value)
            ' </Snippet2>
        End If
    End Sub
End Module
' The example displays the following output:
'       http:8080

See also