Share via


如何:从 URL 中提取协议和端口号

更新:2007 年 11 月

下面的示例使用 Match.Result 从 URL 提取协议和端口号。

示例

Dim url As String = "https://www.contoso.com:8080/letters/readme.html" 
Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", _
      RegexOptions.Compiled)
Console.WriteLine(r.Match(url).Result("${proto}${port}"))
' The example displays the following output:
'       http:8080
string url = "https://www.contoso.com:8080/letters/readme.html";

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

请参见

其他资源

.NET Framework 正则表达式