Regex negative lookahead in c#

我只是一个虾纸丫 提交于 2019-12-12 14:38:08

问题


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?


回答1:


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

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




回答2:


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

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



回答3:


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

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


来源:https://stackoverflow.com/questions/5081504/regex-negative-lookahead-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!