Break down your regex to see why it doesn't work.
.*?(?<columnname>.+?)(?<operator>=|<|>|<>)(?<value>.+?)\s
.*?
— 0 or more characters, non-greedy.
(?<columnname>.+?)
— 1 or more characters, which can be anything, non-greedy, capture into columnname
.
(?<operator>=|<|>|<>)
— One of =
, <
, >
, or <>
, capture into columnname
(?<value>.+?)
— 1 or more characters, which can be anything, non-greedy, capture into value
\s
— Must match a whitespace character here
The only restrictions here are what you expect to find in operator
and the single whitespace at the end of the string. Are you feeding the entire query into the regex, or just the string you gave as an example?
If you match this against the example string you gave
col1 = 5 and col1 < 6 or col3 <> ?
you will find that you have a single space in columnname
, then an equals sign in operator
, then another single space and the number 5
in value
.
Making this work as you wish is problematic because you might have spaces in your identifiers. Something like this might work:
.*?(?<columnname>`.+?`|\S+)\s*(?<operator>=|<|>|<>)\s*(?<value>`.+?`|\S+)
The only changes I made here are that columnname and value must either be all non-whitespace, or they must be values surrounded by backticks. Also, there are optional spaces between the captures, and not a required one at the end.
You might consider sharing your C# as well so that we can see how you are invoking the regex against the string, as given the regex will only match one column/operator/value set.