利用C#正則表達式提取相關URL中的域名
作者:楊逍
下面介紹的是一個利用C#正則表達式提取網頁URL中域名的函數,這個小小的案例能給大家帶來幫助,就是對筆者的獎勵。
說來慚愧,曾經形式語言學習時考了90多分,寫程序時卻連一個最基本的C#正則表達式提取也構造不出來。
下面是一個利用C#正則表達式提取網頁URL中域名的函數:
- view plaincopy to clipboardprint?
- private string regexdom(string url)
- {
- string text = url;
- string pattern = @"(?<=http://)[\w\.]+[^/]"; //C#正則表達式提取匹配URL的模式,
- string s = "";
- MatchCollection mc = Regex.Matches(text, pattern);//滿足pattern的匹配集合
- foreach (Match match in mc)
- {
- s = match.ToString();
- }
- return s;
- //textBox1.Text = s;
- }
【編輯推薦】
責任編輯:彭凡
來源:
CSDN