PHP-regexp忽略大括号之间的所有内容(甚至大括号)


PHP-regexp ignore everything (even curly braces) between curly braces

请帮忙,我怎么能检索text0和text4在主题下面:

text0{tex1{text2}text3}text4

锚定!

$text = 'text0{tex1{text2}text3}text4';
if (preg_match('/^('w+){['w{}]+}('w+)$/', $text, $matches))
{
    list(,$start, $finish) = $matches;
}

$start将为"text0", $finish将为"text4"

如果你想要括号外的内容,你可以使用:

preg_match_all('~(?<out>[^{]++)|({(?>[^{}]++|(?2))*+})~', $string, $matches);
print_r($matches['out']);