如何正确转义 Match.Regex 中的引号
本文关键字:Regex 何正确 转义 Match | 更新日期: 2023-09-27 18:30:18
Testbed: http://regexhero.net/tester/
输入字符串:"_sql": "SELECT * FROM AA"
正则表达式:(?:"_sql"['s]*)([^,]*)
期望结果:"从 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
是假的?
你忘了在你的字符串周围加上引号" "
"。
尝试-
Match match = Regex.Match("'"_sql'": '"SELECT * FROM AA'"", @"(?:""_sql""['s]*)([^,]*)");
你可以有一个单独的变量,并正确地转义它。
string newVa = "'"_sql'": '"SELECT * FROM AA'"";
Match match = Regex.Match(newVa , @"(?:""_sql""['s]*)([^,]*)");
或
Match match = Regex.Match(@"""_sql"": ""SELECT * FROM AA""", @"(?:""_sql""['s]*)([^,]*)");