开发者

Regex negative lookahead in c#

开发者 https://www.devze.com 2023-02-12 09:58 出处:网络
I need to match [\"this\" but not :[\"this\" I have this code: Match match = Regex.Match(result, @\"\\[\"\"(.*?)\"\"\",

I need to match ["this" but not :["this"

I have this code:

        Match match = Regex.Match(result, @"\[""(.*?)""",
            RegexOptions.IgnoreCase);

        while (match.Success)
        {
            MessageBox.Show(match.Groups[1].Value.Trim());
        }

I have tried the pattern @"(?!:)\[""(.*?)""", 开发者_开发百科 but it still match :["this". Whats the pattern I need to achieve this?


You are looking ahead (rightwards in the string) when you want to be looking behind (leftwards in the string).

Try @"(?<!:)\[""(.*?)""" instead.


I used RegexBuddy (I love that app) set to .NET and got the following expression:

@"(?<!:)\[""(.*?)"""


You're doing a negative lookahead when you should be doing a negative lookbehind. Try this instead:

Match match = Regex.Match(result, @"(?<!:)\[""(.*?)""", RegexOptions.IgnoreCase);
0

精彩评论

暂无评论...
验证码 换一张
取 消