?
Solved

C# Regular Expression

Posted on 2012-08-13
9
Medium Priority
?
324 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:supreeths84
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38289834
I'd try matching with pattern:

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

and looking at the 3 resulting groups.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38289839
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

0
 

Author Comment

by:supreeths84
ID: 38289840
@TerryAtOpus: Can you please help me with some example code?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 38289844
Note that I'm assuming that values like these won't be present:
><
==
<<
>>
=<
=>
as the pattern will consider those valid.
0
 

Author Comment

by:supreeths84
ID: 38289850
No, the symbol would be <,=,>,<>,<=,>=
0
 

Author Comment

by:supreeths84
ID: 38289866
@kaufmed: The regular expression doesnt work for <>
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38289874
Perhaps try (building on kaufmed's code):

Match m = Regex.Match(value, "(.*?)(\\<=?|=?>|=|<>)(.*?)");
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38289913
Move the <> to the beginning:

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


[edit]

I screwed up ">=". Corrected above.

[/edit]
0
 

Author Comment

by:supreeths84
ID: 38289992
Thanks Kaufmed. It works.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question