use strict; my $text='(one dfd(two(three)four()(five)df())df)'; print $text if (my @x = $text =~ m/\(/g) == (my @y = $text =~ m/\)/g);