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?


6
  • Can you show how you include the regex in the C# code? Commented Feb 5, 2014 at 12:44
  • Probably you are not doubling (escaping) \? like \\s? Commented Feb 5, 2014 at 12:48
  • I added @ so I don't think that this is the reason Commented Feb 5, 2014 at 12:50
  • 2
    The problem is and ' are different code points. Commented Feb 5, 2014 at 12:51
  • 1
    For the updated question, you could use an sql parser, stackoverflow.com/questions/7442311/… Commented Feb 5, 2014 at 13:09

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.