문제

Given some text I want remove all text wrapped between { and }. The block may themselves contain some block. How can I do that using PCRE regex?

\\example input:
{a}b{c{d}}e{f{g}h}

\\output:
be
도움이 되었습니까?

해결책

Using regular expression recursion:

\{(?>[^{}]|(?R))*\}

See a regex101 demo


PHP example:

$input = '{a}b{c{d}}e{f{g}h}';
$output = preg_replace('/\{(?>[^{}]|(?R))*\}/', '', $input);
echo($output); # => be
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top