$_ = "{a{12}s{ss}}"; my $myre; $myre = qr/ { (?: (?>[^{}]+) | (??{$myre}) )* }/x; my ( $match ) = /($myre)/; print $match;