I am trying to create a regular expression that understands mathematical equations (>, <, =, <=, >=, !=). Which is something pretty simple. I have come up with:
/(.*)([!<>][=]|[<>=])(.*)/
But when I use this regex in PHP, with preg_match, if equation is XYZ!=ABC, it just matches with =. Shouldn’t it match the first expression it found from left to right, which is currently !=? If mine solution is wrong -which seems so-, could anyone tell me why?
Thanks in advance.
Make the
(.*)lazy;(.*?), it will match the fewest possible characters before it can continue.What you have now is greedy, so .* will match as many characters as it can to complete the expression, the longest that can match the first part is
XYZ!, and then it needs to match the=in the second piece to continue.