Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to write a regular expression in C# that can test some text or strings. I want the expression to have relational statements included < | > |<=|>= | == | != ??

C#
Myexpression = new Regex("^[a-zA-Z_][a-zA-Z0-9_] (< | > | <=| >= | == | !=) ([a-zA-Z_][a-zA-Z0-9_] | \d*) *$");


I want this expression to match any statement that compares two variables or one variable with one digit value, but it does not work as expected:(


Help me Please!
Posted
Updated 3-May-11 6:57am
v3
Comments
yesotaso 3-May-11 15:03pm    
Can you give some samples? If you insist on Regular Expressions of course.
Like:
Johann Sebastian Bach - false
Wolfgang Amadeus Mozart - false
Manowar - true
it doesnt have to be that obvious :)
Pete O'Hanlon 3-May-11 15:26pm    
I've updated my answer to include another possible solution.

Remember that every character is significant in a regex - including spaces. This will probably do a bit better, but...
public static Regex regex = new Regex(
      "[a-zA-Z_][a-zA-Z0-9_]\\s*(<=|>=|==|!=|<|>)\\s*[a-zA-Z_][a-zA"+
      "-Z0-9_]",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.Singleline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
To be honest, I get the feeling you are trying to twist a regex to do something that would be better done with tokens and syntax checking of some form.

However, if you are going to continue, I strongly suggest you get a copy of Expresso[^] - it's free, and it really helps with designing, explaining and test regexes.
 
Share this answer
 
Comments
nenalkhun 3-May-11 15:02pm    
mmmmm...
i have a text field where the user can input text, this text should be like a comparison statement, he should input something that has variable compared to a variable or a variable compared to a number
Ex. x > Y
or
X > 3

that all i want !!
OriginalGriff 3-May-11 15:11pm    
I probably still wouldn't go with a regex - and I like regexes!
If you use a regex, then you still need to check afterwards what you have matched, and still have to check he hasn't done something silly, like leave out spaces, or forgotten what operators are, or mean?
Why not make it easier for him: a two character max text box, a group of radio buttons, a two character max text box. he can't get it quite so wrong, and your life is easier...
Kim Togo 3-May-11 15:07pm    
My 5. Regular expression can do many things. I also will recommend Expresso, a really god tool for building and testing regular expression.
As OriginalGriff states, this isn't the best target for a regular expression. The regular expression you are trying to put together here allows you to do things like a_ == b_ which might not be exactly what you want. It also limits you to two character variable names.

Rather than trying to twist the regular expression and bend your brain into uncomfortable shapes in the process, I'd advise you to consider using an alternate technique such as a lexical analyser. There are several freely available lexical analysers, but the one people seem to gravitate towards is ANTLR[^].

Alternatively, you could really go the whole hog and create your own DSL (Domain Specific Language). Microsoft has examples of how to do this in the Visual Studio SDK.

[Edit for OP]
An alternative method would be to roll your own quick and dirty parser. Basically, you'd strip out any spaces then search for the first none alphanumeric character (that wasn't an underscore); this would be the start of your comparison operator. Then, you'd search for the first alpha-numeric (or underscore) character. At this point, you have the indices of where the variables are, and where the operator is.
 
Share this answer
 
v2
Comments
nenalkhun 3-May-11 15:02pm    
mmmmm...
i have a text field where the user can input text, this text should be like a comparison statement, he should input something that has variable compared to a variable or a variable compared to a number
Ex. x > Y
or
X > 3

that all i want !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900