Regex match double curly braces nested

故事扮演 提交于 2020-01-06 20:04:45

问题


would regex matches while/loop double curly braces contains double curly braces?

<?php
$str = '<html lang="{{var doc-lang}}">
<head>
<title>{{var doc-title}}</div>
</head>
<body>
<div class="container">
    <div class="row" {{while products}}>
        {{var name}}
        {{var sku}}
        {{var barcode}}
    </div {{while end}}>
</div>
</body>
</html>';

Can we get

<div class class="row"', [products], {{var name}}, {{ var sku}} and {{var barcode}}

from $str?

I only can think of this Reg

\<((.*)\{\{while\s+(.*)\}\}(.*)\{\{while\s+end\}\})\>

Regex101


回答1:


Try this:

/\s*(.*\{\{while\s+.*(?:\r?\n.*)+\{\{while\s+end.*)/

Online Demo


Also to get them separately (as you mentioned in the question and comment), Try this:

/\s*(?:(.*)\s+\{\{while\s+(.*)\}\}.*\r?\n\s*(.*)\r?\n\s*(.*)\r?\n\s*(.*)\r?\n.*)/

And you can get what you need like this:

'$1', [$2], $3, $4 and $5

Output:

'<div class class="row"', [products], {{var name}}, {{ var sku}} and {{var barcode}}

Online Demo



来源:https://stackoverflow.com/questions/35505957/regex-match-double-curly-braces-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!