【发布时间】:2014-05-24 20:45:51
【问题描述】:
Regex 粉丝们,大家好。 上次,Casimir 和 Hippolyte 找到了一个优雅的解决方案来解决我的问题。
Regex: matching open/close tags which accepts another open/close tag with same name
从他(她?)正则表达式开始,程序发生了一些变化, 我可以设法找到一个可行的解决方案。 但是,我并不完全满意。
问题是现在有两种类型的组件:
- 开始标签以加号 (+) 结尾的那些
- 开始标签以减号 (-) 结尾的那些
但是,它们都具有相同的结束标记。 此外,两种类型的组件都可以包含另一种类型(加号可以包含减号,反之亦然)。
我只需要获取“加组件”的内容。
<?php
$subject = '
{{poo+}} # T1
Hello
{{poo-}} # T2
Nested 1
{{/poo}} # T3
{{/poo}} # T4
{{poo+}} # T5
Bye
{{/poo}} # T6
';
// The solution below works, but I'm forced to capture all types of components.
// I can differentiate them later using php...but I'm looking for a regex that does that immediately.
//
// The reason why is that in the real program, there are three components types, and the syntax is
// slightly more complex (so the regex would be slower to try all three types of components than just one),
// and there could be more component instances.
$p = '`(?x)
{{(\w+)([+-])}}
# ( # you need probably this capture group later
(?>
[^{]++
|
{ (?!{)
|
{{ (?! /? \1 \b) # if needed you can add }} in the lookahead
|
(?R)
)*
# )
{{/\1}}
`';
preg_replace_callback($p, function($match){
var_dump($match);
}, $subject);
【问题讨论】: