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.
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.
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;
}
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.
><
==
<<
>>
=<
=>
as the pattern will consider those valid.
ASKER
No, the symbol would be <,=,>,<>,<=,>=
ASKER
@kaufmed: The regular expression doesnt work for <>
Perhaps try (building on kaufmed's code):
Match m = Regex.Match(value, "(.*?)(\\<=?|=?>|=|<>)(.*? )");
Match m = Regex.Match(value, "(.*?)(\\<=?|=?>|=|<>)(.*?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Kaufmed. It works.
(.*?)([<=>]{1,2})(.*?)
and looking at the 3 resulting groups.