use warnings; use strict; my $node = 'workset((ab;joiret;garg)) c wasdobao; erhgahufdgah; c workset((adsghlia) c aghaoeriarg;oi c aasdfgohaerg c workset(empty) c ah;sorguiaerg c aoi;hgruio;ghaer c playA c dIonly '; my $modified = join q{ } => reverse split /\s+/ => $node; # you could print modified to see the reversed string if ( my @dat = $modified =~ /\(\(?(.+?)\)/g ) { print "@dat\n"; # prints empty adsghlia ab;joiret;garg }