use strict; my $text='(one dfd(two(three)four()(five)df())df)'; print $& if $text=~m#(\([^()]*(\([^()]*\))+[^)]*\))+#; #### (two(three)four() #### (one dfd(two(three)four()(five)df())df)