Regex negative lookahead in c #

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 tried the template @"(?!:)\[""(.*?)""", but it still matches :["this". What picture do I need for this?

+3
source share
3 answers

You look ahead (to the right of the line) when you want to look (to the left of the line).

Give it a try @"(?<!:)\[""(.*?)""".

+5
source

I used RegexBuddy (I love this application) installed in .NET and got the following expression:

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

You make a negative look when you have to do a negative lookbehind. Try instead:

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

Source: https://habr.com/ru/post/1793924/


All Articles