Regex to match placeholders

Let’s say I have a twig template and I would like to match every placeholder occurence


Hi {{ user.name }},
this is just an {{ example.name }}

If I use in php this regex: /{{2}([^}]+)}{2}/

I will obtain as output not only the placeholders but even the content (so, basically, this)

array(
 array(
   0 => {{ user.name }}
   1 => {{ example.name }}
 ),
 array(
   0 => user.name
   1 => example.name
 )
);

How can I isolate only the whole placeholder? (So, basically, how can I obtain only first slug of array?)


Source: regex

Leave a Reply