Define rule type in string and argument arguments

I have a line containing a filter rule:

String rule = "%john"

Available rule types:

Equal = "value"
StartsWith = "value%"
EndsWith = "%value" 
In = "in(value1,value2,value3)"  
Range = [min;Max]  
Min = [min;]  
Max = [;Max]  

Denial: one of the previous! before. for example: "! [min, Max]"

Given one of these rules, how to determine the type of rule (equal, range, etc.) and get the parameters of this rule (value, min, max, etc.)?

Should I use Regex? I am not very familiar with Regex.

+4
source share
1 answer

Well, in the simplest case (without escape characters), no stranger cases (for example, when you [1;2]should consider it as "equal to the string" [1; 2] ", you can use only regular expressions:

  // Please notice the order! E.g. check for Equals last
  List<KeyValuePair<String, String>> rules = new List<KeyValuePair<string, string>>() {
    new KeyValuePair<String, String>("Max", @"^(?<Negation>!*)\[;(?<Value>.+?)\]$"),
    new KeyValuePair<String, String>("Min", @"^(?<Negation>!*)\[(?<Value>.+?);\]$"),
    new KeyValuePair<String, String>("Range", @"^(?<Negation>!*)\[(?<Value>.+?;.+?)\]$"),
    new KeyValuePair<String, String>("In", @"^(?<Negation>!*)in\((?<Value>.+?(,.+?)*)\)$"),
    new KeyValuePair<String, String>("StartsWith", @"^(?<Negation>!*)%(?<Value>.+?)$"),
    new KeyValuePair<String, String>("EndsWith", @"^(?<Negation>!*)(?<Value>.+?)%$"),
    new KeyValuePair<String, String>("Equals", @"^(?<Negation>!*)(?<Value>.+?)$"),
  };

  String filter = "!!in(abc,def,xy,pq)"; // double negations cancel each other

  foreach (var rule in rules) {
    Match match = Regex.Match(filter, rule.Value);

    if (match.Success) {
      String report = String.Format("RULE: \"{0}\"; Negation: \"{1}\"; Value: \"{2}\"",
        rule.Key,
        match.Groups["Negation"].Value.Length % 2 != 0,
        match.Groups["Value"].Value);

      Console.Write(report);

      break;
    }
  }

Output signal

: ""; : ""; : "abc, def, xy, pq"

, Split:

// ["abc", "def", "xy", "pq"]
String[] parts = match.Groups["Value"].Value.Split(',');

, escape- ..,

: , , .

  Types - int    (e.g. 123)      
          float  (e.g. -1.23e-54)
          string (e.g. abc, "123", ab%c, ab_c, ab\%c, ab\_c, 100\%, ab\"c, ab\\c, "in")  

, , _ %; , integer/float, " (, 100 "100" - ). , \ escape- (, , \" "\"")

  Key words =        in
  Special symbols =  [ ] ( ) , ; % _

  equals   (including wild cards)
  in       (including wild cards)
  range    (min, max, range) 
  negation (the only boolean operation)

:

  • Tokenizer
  • Parser
  • Builder

.

  !!in (12%3, ab\%c, pq_x%)

  Tokenization: "!", "!", "in", "(", "12%3", "ab\%c", "pq_x%", ")"
  Parsing:      "negation", 
                "negation", 
                 "in" (with three arguments: "12%3", "ab\%c", "pq_x%")
  Validating:   syntax is valid 
  Builder:      My_Field like '12%3' or 
                My_Field like '12\%3' escape '\' or
                My_Field like 'pq_x%'
+2
source

All Articles