0

I'm trying to write regular expression that should get only the following patterns:

WordWihoutNumbers.WordWihoutNumbers='value'

and patterns with multiple sub expressions like:

WordWihoutNumbers.WordWihoutNumbers='value' OR WordWihoutNumbers.WordWihoutNumbers='value2' AND WordWihoutNumbers.WordWihoutNumbers='value3'

WordWihoutNumbers must be at least two characters and without digits.

for example, those are valid string:

  • Hardware.Make=’Lenovo’
  • Hardware.Make=’Lenovo’ OR User.Sitecode=’PRC’

and those are not:

  • Hardware.Make=’Lenovo’ OR => because there is nothing after the OR operator
  • Hardware.Make=’Lenovo => ' missing
  • Hardware Make=’Lenovo => . missing
  • Hardware.Make’Lenovo' => = missing

I used RegexBuddy to write the following Regex string:

(?i)(\s)*[a-z][a-z]+(.[a-z][a-z]+)(\s)*=(\s)*'[a-z0-9]+'(\s)*((\s)*(AND|OR)(\s)*[a-z][a-z]+(.[a-z][a-z]+)(\s)*=(\s)*'[a-z0-9]+')*

When I tested it using RegexBuddy it worked fine but when I using it inside my C# code I'm always getting 'false' result.

What am I'm doing wrong?

This is what I did in my C# code:

string expression = "Hardware.Make=’Lenovo’ OR User.Sitecode=’PRC’";
Regex expressionFormat = new Regex(@"(?i)(\s)*[a-z][a-z]+(.[a-z][a-z]+)(\s)*=(\s)*'[a-z0-9]+'(\s)*((\s)*(AND|OR)(\s)*[a-z][a-z]+(.[a-z][a-z]+)(\s)*=(\s)*'[a-z0-9]+')*");
bool result = expressionFormat.IsMatch(expression );

and result parameter is always false


UPDATE: thanks to @nhahtdh for his comment, I used a in my input checking instead of ' I need to add to this expression also parenthesis validation, for example: ((WordWihoutNumbers.WordWihoutNumbers='value' OR WordWihoutNumbers.WordWihoutNumbers='value2') AND WordWihoutNumbers.WordWihoutNumbers='value3') is valid but

)WordWihoutNumbers.WordWihoutNumbers='value' OR WordWihoutNumbers.WordWihoutNumbers='value2') AND WordWihoutNumbers.WordWihoutNumbers='value3') is invalid.

Is it possible to implement using Regex? do you have an idea?


Ofir
  • 5,049
  • 5
  • 36
  • 61

1 Answers1

0

Thanks to @nhahtdh that found my issue.

the problem was that and ' are different code points and that was the reason my regular expression didn't work (it was input problem).

Ofir
  • 5,049
  • 5
  • 36
  • 61