I am trying to write a replacement regular expression to surround all words in quotes except the words AND, OR and NOT.
I have tried the following for the match par
Call me crazy, but I'm not a fan of fighting regex; I limit my patterns to simple things I can understand, and often cheat for the rest - for example via a MatchEvaluator:
string[] whitelist = new string[] { "and", "not", "or" };
string input = "foo and bar or blop";
string result = Regex.Replace(input, @"([a-z0-9]+)",
delegate(Match match) {
string word = match.Groups[1].Value;
return Array.IndexOf(whitelist, word) >= 0
? word : ("\"" + word + "\"");
});
(edited for more terse layout)