@foo = ('a(b(c)d(e(f)g)h)i(j(k)l(m)n)o', 'a(b(c)d(e(f)g)h)i(j(k)l(m)n)o'); for (@foo) { print "\'$_\' has "; while s/[(][^()][)]//; # get rid of matched () print "un" if /[()]/; # see if any unmatched print "balanced parentheses" }