I have the string bellow:
$string = '
@if(topLevel)
{{
<form method="post">
@if(bottomLevel)
{{
<input>
}}
</form>
}}';
What i’m trying is to find the value inside the @if(.*?) and the content between the brackets of the top level “@if”.
This is the pattern i’ve tried:
@if((.*?))(s|n|t)+{{(s|n|t)(.*?)(s|n|t)}}
This is matching:
@if(bottomLevel)
{{
<input>
}}
How can i go from the outside into the string and match the top level “@if”?
Advertisement
Answer
Use singleline flag, and remove lazy from .*?:
@if((.*?))s+{{s*(.*)s*}}
And since TAB is included in s, and with the singleline flag n as well, you only need s to match the white space.
Edit
Here’s an alternative using recursion, to manage multiple groups of braces.
@if((.*?))s+{{((?:[^{}]|(?R))*)}}