简体   繁体   English

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

[英]How do I escape quotation marks in Match.Regex properly?

Testbed: http://regexhero.net/tester/ 试验台: http//regexhero.net/tester/

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

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

Now I went into c# and coded this up: 现在我进入c#并编写了这个代码:

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

Why is the match.Success false? 为什么match.Success false?

You have forgot to put quotes " " around your string'. 你已经忘了引号" "在你的字符串”。

Try- 尝试-

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

You can have a separate variable, and properly escape it. 你可以有一个单独的变量,并正确地逃避它。

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM