Search code examples
phpregexpreg-matchlazy-evaluationquantifiers

Make regex pattern stop at the shortest match instead of the longest match


I have a problem with one regex expression to be used so i.e. the input string looks like

hello world and me or you

and I would like to match all from hello until the closest/nearest of the noisy words: and,or

so far I have come up with something like that:

preg_match_all("/^hello[A-Z0-9 -]*(or|and)/is",$string,$match);

but the problem is that it will return: hello world and me or instead of hello world and since the or is first in (or|and) list.

It would be really appreciated if anyone could tell me is there an option to tell regex engine to check which one is closer/nearer from the OR tokens list to match and used that one instead of checking the order as provided i.e. (or|and) in which case and should be used as its closer to initial pattern.

P.S. changing an order inside (or|and) is not a solution as there are more words and you never know which one is nearer so it must be done on the algorithmic level.


Solution

  • The question mark after an asterisk (ie. /.*?/) tells the asterisked expression to be not greedy. So your RegExp should be /^hello[A-Z0-9 -]*?(or|and)/is or something similar.