asp.net.ph

Skip Navigation Links

Example: Extracting URL Information

ASP.NET Base Types   Regular Expressions   Regular Expression Examples


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

 [ VB ]
   Function Extension ( url As String ) As String
      Dim r As New Regex ( "^ ( ?<proto>\w+ ) :// [ ^/ ] +? ( ?<port>:\d+ ) ?/", _
         RegexOptions.Compiled )
      Return r.Match ( url ).Result ( "${proto}${port}" )
   End Function
 [ C# ]
   String Extension ( String url )
   {
      Regex r = new Regex ( @"^ ( ?<proto>\w+ ) :// [ ^/ ] +? ( ?<port>:\d+ ) ?/",
         RegexOptions.Compiled );
      return r.Match ( url ).Result ( "${proto}${port}" ); 
   }



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note