【问题标题】:How to get URL from the text [duplicate]如何从文本中获取 URL [重复]
【发布时间】:2013-05-27 20:58:43
【问题描述】:

如何从文本中获取 URL?

例如:

string Text = "this is text with url http://test.com";

我必须获取 url 并将其设置为其他变量。如何使用 ASP.NET 做到这一点?

【问题讨论】:

标签: c# asp.net


【解决方案1】:

String.Split 方法(String[]、StringSplitOptions)

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

您可以找到示例:C# - C++ - F# - VB

[ComVisibleAttribute(false)]
公共字符串[] 拆分(
字符串[] 分隔符,
StringSplitOptions 选项
)

在这种情况下,“http://”可以是一个很好的分隔符字符串。

【讨论】:

    【解决方案2】:

    除了前面的答案,你也可以这样做:

    string text = "this is text with url http://test.com";
    Match match = Regex.Match(text, @"http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$");
    
    // gets you http://test.com
    string url = match.Value;
    

    【讨论】:

      【解决方案3】:
      reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
      

      这是从文本中查找 URL 的正则表达式。

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:
        string _Url = "this is text with url http://test.com";
        
        MatchCollection _Match = Regex.Matches(_Url , @"http.+)([\s]|$)");
        string _Address= _Match[0].Value.ToString();
        

        【讨论】:

          【解决方案5】:

          你可以用

          "/(http|https|ftp|ftps)\://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/\S*)? /"

          作为搜索的正则表达式... ;)

          【讨论】:

            【解决方案6】:
            string url = Text.Substring(Text.IndexOf("http://"));
            

            【讨论】:

              猜你喜欢
              • 2012-02-25
              • 2016-10-14
              • 1970-01-01
              • 2014-01-12
              • 2017-02-26
              • 2022-01-18
              • 2021-11-21
              • 2023-03-24
              • 2017-09-14
              相关资源
              最近更新 更多