Link to home
Start Free TrialLog in
Avatar of supreeths84
supreeths84

asked on

C# Regular Expression

I have a string which is like
field1 - field2 '<=>' 20. Any one of  <=, <, = , >=, >,<> can be present in the '<=>'. I have to split the string to give me the LHS, RHS and the symbol, which can be one of the 6 mentioned above. Is it possible to do this using Regular Expressions or should I write custom code?
Ex: field1 - field2 <= 20 to my parser should give me field1 -field2, <= and 20 as output.
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

I'd try matching with pattern:

(.*?)([<=>]{1,2})(.*?)

and looking at the 3 resulting groups.
Try:

string value = "field1 - field2 <= 20";
Match m = Regex.Match(value, "(.*?)(<=?|=?>|=|<>)(.*)");

if (m.Success)
{
    string LHS = m.Groups[1].Value;
    string OP = m.Groups[2].Value;
    string RHS = m.Groups[3].Value;
}

Open in new window

Avatar of supreeths84
supreeths84

ASKER

@TerryAtOpus: Can you please help me with some example code?
Note that I'm assuming that values like these won't be present:
><
==
<<
>>
=<
=>
as the pattern will consider those valid.
No, the symbol would be <,=,>,<>,<=,>=
@kaufmed: The regular expression doesnt work for <>
Perhaps try (building on kaufmed's code):

Match m = Regex.Match(value, "(.*?)(\\<=?|=?>|=|<>)(.*?)");
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Kaufmed. It works.