.NET Regex for "not this line"

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; } 
+4
source share
1 answer

Randall Schwartz Rule : Use capture in Regex.Match when you know you want to save, and use Regex.Split when you know what you want to throw.

You wrote that you want "everything except", therefore

 var input = "1and(2or3)AND(4OR5)an(6o7)AN(8O9)"; foreach (var s in Regex.Split(input, @"[\d()]|AND|OR", RegexOptions.IgnoreCase)) if (s.Length > 0) Console.WriteLine("[{0}]", s); 

Conclusion:

  [an]
 [o]
 [AN]
 [O] 

To get offsets, keep the delimiters by enclosing the regular expression in parentheses:

 var input = "1and(2or3)AND(4OR5)an(6o7)AN(8O9)"; string pattern = @"([\d()]|AND|OR)"; int offset = 0; foreach (var s in Regex.Split(input, pattern, RegexOptions.IgnoreCase)) { if (s.ToLower() == "an" || s.ToLower() == "o") Console.WriteLine("Found [{0}] at offset {1}", s, offset); offset += s.Length; } 

Conclusion:

  Found [an] at offset 19
 Found [o] at offset 23
 Found [AN] at offset 26
 Found [O] at offset 30 
+6
source

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


All Articles