use Data::Dumper; my @test = ( "1 This (is a test) with good parens", "2 This is a (test with broken a paren", "3 And this would be one) the other way", "4 Lastly, no parens" ); print Dumper \@test; foreach (@test) { my ($ip) = $_ =~ m#\((.*?)\)#; print " Match in parens: $ip\n" if $ip; my ($rp) = $_ =~ m#([^(]*?)\)# if $_ !~ /\(/; print " Match before right paren: $rp\n" if $rp; my ($lp) = $_ =~ m#\((.*)# if $_ !~ /\)/; print " Match after left paren: $lp\n" if $lp; } Output: Match in parens: is a test Match after left paren: test with broken a paren Match before right paren: 3 And this would be one