$braces = qr/(?\{ ([^\{\}]++ | (?&braces))*+ \} )/x; if ($h =~ s/class \s+ (? \w+) \s* (\: (\s* \w*)? \s* (? \w+))? \s* $braces//x) { print "hi\n"; }