I am new to regex and need one expression that:
matches "an" and "AN", but not "and" or "AND" and matches "o" and "O", but not "or" or "OR" in this predicate:
1 and (2or3) and (4OR5) ap (6o7) A.N. (8O9)
Basically, I can't figure out how to convert an expression:
var myRegEx = Regex("[0-9 ()]|AND|OR")
in the expression "everything but" is case insensitive.
You cannot use the word boundary function of regular expressions because the predicate does not require spaces.
(Added after two answers already provided): I also need to know the match index, which is why I'm assuming I need to use Regex.Match ().
Thanks!
Here is what I ended up with:
private bool mValidateCharacters() { const string legalsPattern = @"[\d ()]|AND|OR"; const string splitPattern = "(" + legalsPattern + ")"; int position = 0; string[] tokens = Regex.Split(txtTemplate.Text, splitPattern, RegexOptions.IgnoreCase); // Array contains every legal operator/symbol found in the entry field // and every substring preceeding, surrounded by, or following those operators/symbols foreach (string token in tokens) { if (string.IsNullOrEmpty(token)) { continue; } // Determine if the token is a legal operator/symbol or a syntax error Match match = Regex.Match(token, legalsPattern, RegexOptions.IgnoreCase); if (string.IsNullOrEmpty(match.ToString())) { const string reminder = "Please use only the following in the template:" + "\n\tRow numbers from the terms table" + "\n\tSpaces" + "\n\tThese characters: ( )" + "\n\tThese words: AND OR"; UserMsg.Tell("Illegal template entry '" + token + "'at position: " + position + "\n\n" + reminder, UserMsg.EMsgType.Error); txtTemplate.Focus(); txtTemplate.Select(position, token.Length); return false; } position += token.Length; } return true; }
Jim c source share