regular expression in php: take the shortest match

前端 未结 5 1554
我寻月下人不归
我寻月下人不归 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条回答
  •  Happy的楠姐
    2021-01-12 06:06

    You want "ungreedy matching": preg_match("/{{(.*?)?}}/", $string).

    Note the first question mark - a regex, by default, is "greedy": given multiple ways to match, it matches as much text as it possibly can. Adding the question mark will make it ungreedy, so if there are multiple ways to match, it will match as few characters as it possibly can.

提交回复
热议问题