use strict; my $string = "foobar1"; for my $regex () { chomp $regex; my ($success, @captures); eval { @captures = $string =~ /$regex/; $success = 1 if @captures; @captures = () if $success and @- == 1; }; print "$regex: "; if ($@) { print "invalid regex\n"; ## optionally print $@ here } elsif ($success and @captures) { print "matched: @captures\n"; } elsif ($success and !@captures) { print "matched (no captures)\n"; } else { print "didn't match\n"; } } __DATA__ foo((( (fo(o))(bar) o(.*)a foobar baz(bar)( blah(foo) (.)$ #### foo(((: invalid regex (fo(o))(bar): matched: foo o bar o(.*)a: matched: ob foobar: matched (no captures) baz(bar)(: invalid regex blah(foo): didn't match (.)$: matched: 1