如何正确地在Match.Regex中转义引号?

时间:2013-09-05 09:42:41

标签: c#

Testbed:http://regexhero.net/tester/

输入字符串"_sql": "SELECT * FROM AA" 正则表达式(?:"_sql"[\s]*)([^,]*)

期望的结果:“SELECT * FROM AA”

现在我进入了c#并编写了这个代码:

    Match match = Regex.Match("_sql\": \"SELECT * FROM AA", @"(?:""_sql""[\s]*)([^,]*)");
    if (match.Success)
    {
        String value = match.Groups[0].Value;
        Console.WriteLine(value);
    }

为什么match.Success为假?

2 个答案:

答案 0 :(得分:2)

您忘记在字符串周围添加引号" "

尝试 -

Match match = Regex.Match("\"_sql\": \"SELECT * FROM AA\"", @"(?:""_sql""[\s]*)([^,]*)");

答案 1 :(得分:0)

你可以有一个单独的变量,并正确地逃避它。

string newVa = "\"_sql\": \"SELECT * FROM AA\"";
Match match = Regex.Match(newVa , @"(?:""_sql""[\s]*)([^,]*)");

Match match = Regex.Match(@"""_sql"": ""SELECT * FROM AA""", @"(?:""_sql""[\s]*)([^,]*)");