簡體   English   中英

C#替換URL正則表達式

[英]C# Replace URL Regex

我正在嘗試從字符串中提取URL,並稍后使用它來創建超鏈接。 我希望能夠執行以下操作:-確定輸入字符串是否包含URL-從輸入字符串中刪除URL-將提取的URL存儲在變量中以備后用

誰能幫我這個?

這是識別常見格式URL的絕佳解決方案,例如:

  • www.google.com
  • http://www.google.com
  • mailto:somebody@google.com
  • somebody@google.com
  • www.url-with-querystring.com/?url=has-querystring

使用的正則表達式為:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/

但是,我建議您訪問http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the-來查看工作示例。

用輸入替換輸入

        string input = string.Empty;
        var matches = Regex.Matches(input,
                      @"/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/");
        List<string> urlList = (matches.Cast<object>().Select(match => match.ToString())).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM