#!/usr/bin/perl use strict; use warnings; use 5.012; # #926110 my $regex1 = qr/foo\d+/; my $regex2 = qr/bar\S+?/; my @string = qw(foo17 barABC nomatch); for my $string(@string) { if ($string =~ m/($regex1)/ && $regex1 =~ /(.*)/s) { say "$1 when string is \"$string\""; } elsif ( $string=($regex2) && $regex2 =~ /(.*)/s ) { say "$1 when string is \"$string\""; } else { say "no match on $string"; } }