tags:

views:

37

answers:

4

I'm trying to create a regex that matches comparisons like this:

= 445
> 5
>= 22
< 3
<= 42
<> 10

I thought that this would work, but it doesn't:

[=|>|<|>=|<|<=|<>]\s\d+

It's very important that the '>' or '<' precede '='. These operators would not be valid:

=<
=>
>>
<<
==

I'm using this to create some dynamic sql so the comparison operators need to valid sql.

Any suggestions?

+3  A: 
(=|>|<|>=|<|<=|<>)\s\d+

or something like: (doesn't really do what you want, it matches all 2character combinations of =<>, but for clearness)

[=><]{1,2}\s\d+

-> when you use the [] braces, it means, that one of the character inside should take place (multiple | defined may lead to undefined behavior, or behavior I'm not aware of)

-> you probably wanted to use simple braces (), where the | has the 'OR' meaning.

Yossarian
The second regex matches =>, == and =<.
EmFi
You should add that the second version is just an approximation and matches things like >< and == as well.
Frank
Note that the second one matches some things the first one doesn't. eg: == >> << >< =< =>
Laurence Gonsalves
This is really close but it also matches =< => which would be invalid. The < or > must precede the equals sign.
Micah
The second example was there only as example of using [] .. I had to flag it as 'not-exactly-what-you-wanted', sorry, corrected now.
Yossarian
The key is that the [] defines a character class- each unique character between brackets is a part of the class. The regex engine sees you defining a class with "=", ">", "<" and then you start repeating yourself. Whereas in the group you define with (), the regex engine treats multiple characters as you'd expect. I'd probably go with Yossarian's first option, as the second will match stuff like ">< 22" and "== 19" which you may not want.
Johrn
+1  A: 

You are using '[' and ']' here. Did you perhaps mean to use '(' and ')'?

Harold Bamford
+2  A: 

The syntax […] denotes a character class. Use (…) for grouping instead:

(=|>|<|>=|<|<=|<>)\s\d+

And here’s a more compact one:

(=|[<>]=?|<>)\s\d+

Or:

(=|<[>=]?|>=?)\s\d+
Gumbo
A: 

This one will do what you're looking for.

(<[=>]?|=|>=?)\s\d+
EmFi
That's horrible! Worse than APL!
martinwguy