如何使用正则表达式提取带引号的字符串?或者如何在c#中使用这个特定的正则表达式("[^"]*\.csv")?

Mar*_*tin 2 c# regex parsing

我试图从HTML响应中提取引用的文本.但是,当我使用正则表达式匹配数据时,我收到错误消息"无法识别的转义序列".代码粘贴在下面.我错过了一些基本的东西吗?


    static private string ParseText2GetLink(string response)
    {
        Match match = Regex.Match( response, @"("[^"]*\.csv")" );

        string key = null;
        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            key = match.Groups[1].Value;
            Console.WriteLine(key);
        }
        return key;
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 6

要在@""字符串中包含双引号,您必须将其加倍:

@"(""[^""]*\.csv"")"
Run Code Online (Sandbox Code Playgroud)

或者您可以使用普通字符串:

"(\"[^\"]*\\.csv\")"
Run Code Online (Sandbox Code Playgroud)

(注意,现在你必须逃避反斜杠.)