您可以匹配嵌套函数,例如:
$pattern = '~(@(?<func>\w++)\((?<param>[^)]*+)\)(?<content>(?>[^@]++|(?-4))*)@end)~';
或没有命名捕获:
$pattern = '~(@(\w++)\(([^)]*+)\)((?>[^@]++|(?-4))*)@end)~';
请注意,如果您将整个模式放在前瞻中,您可以拥有所有嵌套函数的所有内容(?=...)
图案细节:
~ # pattern delimiter
( # open the first capturing group
@(\w++) # function name in the second capturing group
\( # literal (
([^)]*+) # param in the third capturing group
\) # literal )
( # open the fourth capturing group
(?> # open an atomic group
[^@]++ # all characters but @ one or more times
| # OR
(?-4) # the first capturing group (the fourth on the left, from the current position)
)* # close the atomic group, repeat zero or more times
) # close the fourth capturing group
@end
)~ # close the first capturing group, end delimiter