Regex fans, hi again. Last time, Casimir and Hippolyte found an elegant solution to my problem.
Regex: matching open/close tags which accepts another open/close tag with same name
There has been a little change in the program since and starting from his (her?) regex, I could manage to find a working solution. However, I'm not completely satisfied with it.
The thing is that now there are two types of components:
- those which opening tag ends with plus (+)
- those which opening tag ends with minus (-)
However, they both have the same ending tag. Also, both types of components can contain the other type (a plus can contain the minus type and vice versa).
I need to get the content of "plus components" only.
<?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);