views:

160

answers:

2

Hi everyone.

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.

+3  A: 

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.

Jeffrey Aylesworth
Thank you. That thing was blowing my mind up.
mistircek
A: 

* is greedy(.*) first matches XYZ! the = is then matched by the second alternation of ([!<>][=]|[<>=]) so the match holds. The left most solution applies only to the expression as a whole, the rule to note here is that where greedy operators are concerned its first come first served.

Paul Creasey