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
为假?
答案 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]*)([^,]*)");