正则表达式:匹配的打开/关闭标签接受另一个同名的打开/关闭标签 [英] Regex: matching open/close tags which accepts another open/close tag with same name
本文介绍了正则表达式:匹配的打开/关闭标签接受另一个同名的打开/关闭标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正则表达式的粉丝们,嗨.在下面的代码中,有两个父标签(T1-4 和 T5-6)和一个子标签(T2-3).
Regex fans, hi. In the code below, there are two parent tags (T1-4 and T5-6), and one child tag (T2-3).
你怎么搭配T1-4?或者一般来说,你如何匹配父标签?
How do you match T1-4 ? or in general, how do you match the parent tags ?
<?php
$subject = '
{{poo}} # T1
Hello
{{poo}} # T2
Nested 1
{{/poo}} # T3
{{/poo}} # T4
{{poo}} # T5
Bye
{{/poo}} # T6
';
$p = '!{{(\w+)}}(.*){{/\1}}!s'; // matches T1-6, too greedy
$p = '!{{(\w+)}}(.*?){{/\1}}!s'; // matches T1-3, not what I want
$p = '`(?xs) # xtended
{{(\w+)}}
.*?
(?R)? # currently working on this one...
{{/\1}}
`';
preg_replace_callback($p, function($match){
var_dump($match);
}, $subject);
推荐答案
这可能是您要找的:
$p = '`(?x)
{{(\w+)}}
# ( # you need probably this capture group later
(?>
[^{]++
|
{ (?!{)
|
{{ (?! /? \1 \b) # if needed you can add }} in the lookahead
|
(?R)
)*
# )
{{/\1}}
`';
这篇关于正则表达式:匹配的打开/关闭标签接受另一个同名的打开/关闭标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文