-1

我试图在引号之间获取所有内容。引号之间的内容是文件位置。所以它就像 "C:\Users\Documents and Settings\Pictures\mypic.bmp" 我目前使用的正则表达式是:

   "([""'])(?:(?=(\\?))\2.)*?\1"

它可以获取数字和字母,只是它在反斜杠处拆分。有人可以帮助我,以便我可以匹配完整的字符串吗?

提前致谢。

4

2 回答 2

3

我会去:

@"""\s*(.*?)\s*"""

我用来测试它的示例代码:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Match match = Regex.Match(txtString.Text, @"""\s*(.*?)\s*""",
            RegexOptions.IgnoreCase);

        if (match.Success)
        {
            string key = match.Groups[1].Value;
            lblFinal.Text = key;
        }
    }

我们可以修正我的反对票吗?;)

于 2013-01-02T03:32:45.430 回答
2

您可以使用以下正则表达式(在 C# 字符串文字中引用):

string regexPattern = "\\\"(.+?)\\\"";

或者

string regexPattern = @"\""(.+?)\""";

没有 C# 文字转义

\"(.+?)\"

这样匹配的组将是引号内的字符串

如果在http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx进行测试

资源

"C:\Users\Documents and Settings\Pictures\mypic.bmp"

图案

\"(.+?)\"

结果

C:\Users\Documents and Settings\Pictures\mypic.bmp
于 2013-01-02T03:50:03.707 回答