代码之家  ›  专栏  ›  技术社区  ›  nLL

c#正则表达式在之后提取链接=

  •  0
  • nLL  · 技术社区  · 13 年前

    找不到更好的标题,但我需要一个正则表达式从下面的示例中提取链接。

    snip...  flashvars.image_url = 'http://domain.com/test.jpg' ..snip
    

    谢谢

    3 回复  |  直到 13 年前
        1
  •  1
  •   Les    13 年前

    考虑下面的示例代码。它显示了如何从提供的字符串中提取。但我已经在弦上展开了一些。通常,*的使用过于包罗万象(如下例所示)。

    主要的一点是,有几种方法可以满足你的要求,给出的第一个答案使用“环顾四周”,而第二个建议使用“小组”方法。选择主要取决于你的实际数据。

            string[] tests = {
                    @"snip...  flashvars.image_url = 'http://domain.com/test.jpg' ..snip",
                    @"snip...  flashvars.image_url = 'http://domain.com/test.jpg' flashvars2.image_url = 'http://someother.domain.com/test.jpg'",
            };
            string[] patterns = {
                    @"(?<==\s')[^']*(?=')",
                    @"=\s*'(.*)'",
                    @"=\s*'([^']*)'",
                                 };
            foreach (string pattern in patterns)
            {
                Console.WriteLine();
                foreach (string test in tests)
                    foreach (Match m in Regex.Matches(test, pattern))
                    {
                        if (m.Groups.Count > 1)
                            Console.WriteLine("{0}", m.Groups[1].Value);
                        else
                            Console.WriteLine("{0}", m.Value);
                    }
            }
    
        2
  •  0
  •   Marcelo Cantos    13 年前

    一个简单的正则表达式 @"=\s*'(.*)'"

        3
  •  0
  •   Tim Pietzcker    13 年前

    编辑:与您编辑的问题匹配的新正则表达式:

    你需要匹配引号之间的内容,在 = ,对吧?

    @"(?<==\s*')[^']*(?=')"
    

    应该可以。

    (?<==\s*') = ' ,就在我们现在的位置之前(正后方)。

    [^']* 匹配任意数量的非- 角色。

    (?=') 声称比赛在下一场比赛之前停止 .

    @"(?<==\s*')(?=(?:https?|ftp|mailto)\b)[^']*(?=')"