foreach $str (@reversedarray) { if ($str =~ /\bdIonly\b.*?\bworkset\b\(\(([^)]*)/ ) { print "content of workset before dIonly: $1\n"; } }