Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取引号之间的值_C#_Regex - Fatal编程技术网

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);
    }
这里的主要变化是该组现在位于引号内