How to match comparison operators in Regex

I am trying to create a regex that matches these comparisons:

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

I thought this would work, but it is not:

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

It is very important that '>' or '<' precede '='. These statements are not valid:

 =< => >> << == 

I use this to create dynamic sql, so comparison operators must have valid sql.

Any suggestions?

+4
source share
6 answers

I would say that the regular expression given by EmFi is good enough. With some changes, it can accept expressions like

 "currentDate>=2012/11/07&&currentDate<=2012/11/08"; 

or

 "currentDate==2012/11/07"; 

With this modified regular expression

 (<[=>]?|==|>=?|\&\&|\|\|) 

And give it as "valid." Probably very simple, but at least in my case it’s enough

EDIT: Regex has been modified to take comparison operators (<,>,> =, <=, ==) and Boolean operators (& &, ||) similarly to C-like languages

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

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

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

-> when you use curly braces [], this means that one of the characters inside must occur (multiple | defined can lead to undefined behavior or behavior that I don't know about)

-> you probably wanted to use simple curly braces (), where | has the value of "OR".

+3
source

The syntax […] denotes a character class. Use instead of (…) to group:

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

And even more compact:

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

Or:

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

This one will do what you are looking for.

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

just decided it for myself. it matches <,>, <=,> = ,! =, =, <> and not => or = <unfortunately, it still matches β†’. I just check this in my application code.

 ([!<>])?([=>])?(?!<) 
0
source

Now I am in an old article, but I made a case-sensitive regular expression and found only what we need, and I hope this helps someone.

His work for! =, <=,> =, ==, <,> and find it only at the beginning of the line.

 ^(^(!=)?|^(<=)?|^(>=)?|^(==)?|^(<)?|^(>)?)? 
0
source

Source: https://habr.com/ru/post/1302519/


All Articles