tags:

views:

90

answers:

5

Hi, I need to match some operators: =, !=, >, <, <=, >= The string I need to match may be something like: "2=2 OR 33 >= 32 AND 3<5"

What can be the RegEx expression to match this, knowing that - I don't want to receive a '=' match on a '<=' operator - The operators may or may not have spaces surrounding them

Thanks in advance! Alex

+2  A: 

This seems to work:

[<>!]?=|[<>]

It takes: <, > or ! before = (or just =),
OR: < or > on their on.

That said, for a bit more complexity you'll probably need a parser (e.g. if you wanted to support parentheses)

Kobi
+1, elegant solution
Rubens Farias
(I'm refreshing the page, eagered for a downvoter to explain where I'm wrong, and thus, perhaps, learn something new)
Kobi
tested with http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx the source `=, !=, >, <, <=, >=` finds only 4 matches from 6, don't understand why...
serhio
I've tested before testing on http://www.gskinner.com/RegExr/ and it worked well, but I'll check again on c#.
Kobi
@serhio, just tested here with http://www.radsoftware.com.au/?from=RegexDesigner and everything is fine (6 matches)
Rubens Farias
Thanks mate but I went with the more readable solution.As for the parenthesis support, I already have a parser, this code is only to translate conditions from PseudoCode to c# code.
AlexCode
@Kobi: (I wasn't a downvoter) What if you change you regex slightly, making it look like "[<>!]=|[<>=]". I think in this case it'll be as simple to understand as Rubens' one and even more compact.
Igor Korkhov
Alex - you've made the right choice. @Serhio - tested on C#, and it works well. Send -1 to the person who wrote your tool.
Kobi
@Kobi: I just checked Derek Slager's tool, it worked as expected, I think @Serhio did something wrong.
Igor Korkhov
tested with `MatchCollection matches = Regex.Matches("=, !=, >, <, <=, >=", "[<>!]?=|[<>]");` matcehs.Count == 6, so this is OK, apparently derekslager has a problem. Sorry. +1 if edit :)
serhio
Ho, well. I guess I'll go learn something else... :)
Kobi
@serhio: No, Derek doesn't have a problem, it seems that you put newline character after the expression
Igor Korkhov
Kobi, the fact is that your solution looks nicer on my code :)I also gave you a up vote.Thank you all for giving the solutions and testing them :)Cheers!Alex
AlexCode
@Igor: Yeah, now I realized it. thanks.
serhio
Kobi, what about if I need to exclude operator chars that are between quotes? Say: "Alex" = "<Alex>"
AlexCode
+4  A: 

Try this:

(<=|>=|!=|=|>|<)
Rubens Farias
+1 you are right, Kobi's version is elegant, but yours is more readable ... :) both got +1
tanascius
Note that the order of those is important. If the less than sign was first in the list, matching against `<=` would return each character as a separate match.
Max Shawabkeh
yeah @Max, I wrote this way just because that =)
Rubens Farias
I completely agree with tanascius' readability comment, +1 as well.
Kobi
Thanks!I had something like this but without the parenthesis! :)I go with the readability too... it will also be easier to add new operators.
AlexCode
Hey Rubens... What about if I need to exclude operator chars that are between quotes? Say: "Alex" = "<Alex>"
AlexCode
+1  A: 

My proposal: [<>]=?|[!=]?=. Matches any of = != > < <= >= ==.

Tomalak
Nice one. Without `==` it can be a neat `[<>]=?|!?=`
Kobi
My PseudoCode operators can evolve to some more complex like a 'LIKE' operator to compare strings, so I may need to add more operators to the search pattern that may be different from these initial ones.I need a simple pattern that cam evolve with the tool without having to rethink a completely new one each time I want to add suppor for a new operator.
AlexCode
A: 

Are you also trying to get the numbers?if so. . .

/[0-9]+[ ]*(<=|>=|!=|=|>|<)[ ]*[0-9]+/

...anything in the range of 0-9 one or more times, followed by 0 or more spaces followed by your operator, 0 or more spaces and anything in the range of 0-9 one or more times.

This will get the number and the operator

Toymakerii
Here you'll get another problem set: that number can be negative, have decimal places, etc
Rubens Farias
True, in which case it is best to rewrite the regex to match this. I always find it best to be the most complete. If you are looking for complex, real, integer, whole or some other type of number you should write the regex to match that rather than leave it ambiguous and possibly get into trouble later.
Toymakerii
No, I just to identify the operator and its left and right operand and the aggregate operator if any (AND/OR).On my example I only wrote number but my operands can be whatever is valid for the c# compiler to successfully compile, and this is quite anything from simple CLR types to complex custom types.So I don't mind about what the user wrote as an operand as long as it compiles and evaluates right it's good for me.
AlexCode
A: 

[^?!><=]+\s*(?<operator>[><!]?=|[><])\s*[^?!><=]+ + ExplicitCapture

filters >> or === or == ==

serhio
Before searching for operators I first split the expression by logic operator (AND/OR). Between each logic operator there can only be one Rule Condition (Left Operand, Operator, Right Operand).So, if more that one operator is found between logic operators the parser automatically throws an "Invalid Empression" exception.This way I don't need to enforce any validations on the RegEx pattern.
AlexCode
I'm thinking that I may need to enforce one validation tho... I need to skip operators that are between quotes. This may happen if an operand is a string containing operator characters, say: "Alex" = "<Alex>"
AlexCode
just if you `Operand` contains quotes, you need to check if they are closed.
serhio
I can do that validation on c#... if the pattern returns more that one match then I evaluate and exclude the one between quotes, but can't it be enforced on the RegEx pattern? Will it make the pattern much more complex to read?
AlexCode
do with Regex a `Replace` of your interquoted strings by, let's say a special symbol like `¤` or `££` etc. Then analyze your text once again.
serhio