my %checks = ( '123:C' => [ 'A[0-7]' ], '456:D' => [ 'B[8-9]', 'B1[0-5]' ] ); while ( ) { chomp; my $check = $_; my $matches = 0; #-- get the trailing key my $first_part = ""; my $key = ""; if ( $check =~ /(\w+):(\w+:\w+)$/ ) { $first_part = $1; $key = $2; } #-- look up key in our checks hash foreach my $match_pattern ( @{$checks{$key}} ) { if ( $first_part =~ /$match_pattern/ ) { print "Line $_ matches $match_pattern:$key\n"; $matches = 1; } } print "Line $_ doesn't match\n" unless ( $matches ); } __DATA__ A4:123:C B8:456:D B11:456:D X11:456:D #### Line A4:123:C matches A[0-7]:123:C Line B8:456:D matches B[8-9]:456:D Line B11:456:D matches B1[0-5]:456:D Line X11:456:D doesn't match