regular expression in php: take the shortest match

前端 未结 5 1524
我寻月下人不归
我寻月下人不归 2021-01-12 05:23

I\'m trying to do a PHP regular expression but i can\'t find the right way ...

Imagine I have this string: \"hello, my {{name is Peter}} and {{I want to eat chocolat

5条回答
  •  天涯浪人
    2021-01-12 06:11

    The PCRE functions a greedy by default. That means, that the engine always tries to match as much characters as possible. The solution is simple: Just tell him to act non-greedy with the U modifier

    /{{(.+)}}/U
    

    http://php.net/reference.pcre.pattern.modifiers

提交回复
热议问题