C# 获取引号之间的值
如何使用正则表达式获取引号之间的值 例如,我想从函数测试中找到所有参数C# 获取引号之间的值,c#,regex,C#,Regex,如何使用正则表达式获取引号之间的值 例如,我想从函数测试中找到所有参数 <html> test("bla"); print("foo"); test("moo"); </html> 测试(“bla”); 印刷品(“foo”); 测试(“moo”); 结果必须是{“bla”,“moo”}编辑:删除旧代码并生成linq版本 var array = (from Match m in Regex.Matches(inText, "\"\\w+?\"
<html>
test("bla");
print("foo");
test("moo");
</html>
测试(“bla”);
印刷品(“foo”);
测试(“moo”);
结果必须是{“bla”,“moo”}编辑:删除旧代码并生成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));
如果您只想让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);
}
这里的主要变化是该组现在位于引号内