【问题标题】:RegEx to extract link正则表达式提取链接
【发布时间】:2011-05-31 01:16:52
【问题描述】:

我正在寻找一个正则表达式来从 URL 中提取链接。网址如下:

/redirecturl?u=http://www.abc.com/&tkn=Ue4uhv&ui=fWrQfyg46CADA&scr=SSTYQFjAA&mk=4D6GHGLfbQwETR

我需要从上面的 URL 中提取链接 http://www.abc.com

我尝试了正则表达式:

redirecturl\\?u=(?<link>[^\"]+)&

这可行,但问题是它不会在第一次出现 & 之后截断所有字符。

如果你能修改 RegEx 以便我得到链接,那就太好了。

提前致谢。

【问题讨论】:

  • This 可能会有所帮助。

标签: c# regex url


【解决方案1】:
redirecturl\\?u=([^\"&]+)

当它到达 &amp; 或者根本没有 &amp; 时,它应该被截断

【讨论】:

  • 并在匹配集合中取第二组。
【解决方案2】:

使用URI class 怎么样?

例子:

string toParse = "/redirecturl?u=http://www.abc.com/&amp;tkn=Ue4uhv&amp;ui=fWrQfyg46CADA&amp;scr=SSTYQFjAA&amp;mk=4D6GHGLfbQwETR";

// remove "/redirecturl?u="
string urlString = toParse.Substring(15,toParse.Length - 15); 

var url = new Uri(urlString);
var leftPart = url.GetLeftPart(UriPartial.Scheme | UriPartial.Authority);
// leftPart = "http://www.abc.com"

【讨论】:

    【解决方案3】:

    用 \ 转义特殊字符,即匹配/使用 [\/]

    var matchedString = Regex.Match(s,@"[\/]redirecturl[\?]u[\=](?<link>.*)[\/]").Groups["link"];
    

    【讨论】:

      【解决方案4】:
      using System.Text.RegularExpressions;
      
      //  A description of the regular expression:
      //  
      //  [Protocol]: A named capture group. [\w+]
      //      Alphanumeric, one or more repetitions
      //  :\/\/
      //      :
      //      Literal /
      //      Literal /
      //  [Domain]: A named capture group. [[\w@][\w.:@]+]
      //      [\w@][\w.:@]+
      //          Any character in this class: [\w@]
      //          Any character in this class: [\w.:@], one or more repetitions
      //  Literal /, zero or one repetitions
      //  Any character in this class: [\w\.?=%&=\-@/$,], any number of repetitions
      
      public Regex MyRegex = new Regex(
            "(?<Protocol>\\w+):\\/\\/(?<Domain>[\\w@][\\w.:@]+)\\/?[\\w\\."+
            "?=%&=\\-@/$,]*",
          RegexOptions.IgnoreCase
          | RegexOptions.CultureInvariant
          | RegexOptions.IgnorePatternWhitespace
          | RegexOptions.Compiled
          );
      
      
      // Replace the matched text in the InputText using the replacement pattern
       string result = MyRegex.Replace(InputText,MyRegexReplace);
      
      // Split the InputText wherever the regex matches
       string[] results = MyRegex.Split(InputText);
      
      // Capture the first Match, if any, in the InputText
       Match m = MyRegex.Match(InputText);
      
      // Capture all Matches in the InputText
       MatchCollection ms = MyRegex.Matches(InputText);
      
      // Test to see if there is a match in the InputText
       bool IsMatch = MyRegex.IsMatch(InputText);
      
      // Get the names of all the named and numbered capture groups
       string[] GroupNames = MyRegex.GetGroupNames();
      
      // Get the numbers of all the named and numbered capture groups
       int[] GroupNumbers = MyRegex.GetGroupNumbers();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-22
        • 2014-07-15
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        相关资源
        最近更新 更多