I'm a regex newbie and need a single expression that:
matches the "an" and the "AN" but not the "and" or "AND" and matches the "o" and the "O" but not the "or" or "OR" in this predicate:
1and(2or3)AND(4OR5)an(6o7)AN(8O9)
Basically I can't figure out how to convert the expression:
var myRegEx = Regex("[0-9 ()]|AND|OR")
into a "everything but", case insensitive expression.
Can't use the regex word boundaries feature because the predicate isn't required to have spaces.
(Added after two answers were already provided): I also need to know the index of the match, which is why I'm assuming I need to use the Regex.Match() method.
Thanks!
Here's 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;
}