在引号之间获取值

如何使用RegEx获取引号之间的值

例如,我想从function测试中找到所有参数

 test("bla"); print("foo"); test("moo");  

结果必须是{“bla”,“moo”}

如果你只想test args,你需要在正则表达式中包含它:

  StringBuilder sb = new StringBuilder("{"); bool first = true; foreach (Match match in Regex.Matches(html, @"test\((""[^\""]*\"")\)")) { if(first) {first = false;} else {sb.Append(',');} sb.Append(match.Groups[1].Value); } sb.Append('}'); Console.WriteLine(sb); 

从这个问题来看,我在这里使用引用检测。

或者 – 如果您只想要值:

  foreach (Match match in Regex.Matches(html, @"test\(""([^\""]*)\""\)")) { Console.WriteLine(match.Groups[1].Value); } 

这里的主要变化是该组现在在引号内。

编辑:删除旧代码并制作了linq版本…

  var array = (from Match m in Regex.Matches(inText, "\"\\w+?\"") select m.Groups[0].Value).ToArray(); string json = string.Format("{{{0}}}", string.Join(",", array));