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?
(=|>|<|>=|<|<=|<>)\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.
I would say the regex given by EmFi is good enough. With some modifications it can take expressions like this
"currentDate>=2012/11/07&¤tDate<=2012/11/08";
or this
"currentDate==2012/11/07";
With this modified regex
(<[=>]?|==|>=?|\&\&|\|\|)
And give it as "valid". Probably is very simple but at least in my case, enough
EDIT: Regex was modified in order to take comparison operators (<,>,>=,<=,==) and boolean operators (&&,||) in a similar way to C-like languages
The syntax […]
denotes a character class. Use (…)
for grouping instead:
(=|>|<|>=|<|<=|<>)\s\d+
And here’s a more compact one:
(=|[<>]=?|<>)\s\d+
Or:
(=|<[>=]?|>=?)\s\d+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With