简体   繁体   English

有人可以帮我使用正则表达式吗?

[英]Can someone help me with my regex?

I am trying to get everything between quotations. 我试图使报价之间的一切。 What will be between the quotations is file locations. 引号之间是文件位置。 So it will be like "C:\\Users\\Documents and Settings\\Pictures\\mypic.bmp" The regex I am currently using is: 因此,它将类似于“ C:\\ Users \\ Documents and Settings \\ Pictures \\ mypic.bmp”。我当前使用的正则表达式为:

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

It works to get numbers and letters except it splits it at the backslash. 它可以获取数字和字母,但会在反斜杠处将其分开。 Can someone help me out so I can match the full string? 有人可以帮我,以便我可以匹配完整的字符串吗?

Thanks in advance. 提前致谢。

I would go with: 我会去:

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

Sample code I used to test it: 我用来测试的示例代码:

    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;
        }
    }

Can we fix my negative votes? 我们可以修正我的反对票吗? ;) ;)

You can use the following regex (quoting in C# string literal): 您可以使用以下正则表达式(以C#字符串文字形式引用):

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

or 要么

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

Without C# literal escaping 没有C#文字转义

\"(.+?)\"

This way the matched group will be the string within the quotations 这样,匹配的组将成为引号内的字符串

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

Source 资源

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

Pattern 图案

\"(.+?)\"

Result 结果

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

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

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