use strict; use warnings; # init the "valid hash" # in real program, probably K1 etc probably # come from some configuration file my %hash = map{$_ => 1}qw (K1 ASC GEB N1); # I use a simple fixed regex to pick up first XXX_YYY # in the line. If there are multiple of these things # per line, then there are a couple of ways to handle # that. I'd have to see some actual real data to make # a recommendation. Note that \w includes the underscore, # so I did something simple to exclude the _ character. # To check if match worked, you can put whole regex # line in the "if" clause, or just check "definedness" # like I do below. If $tok is defined in the hash, # then it is a "good" one. while (my $line = ) { my ($tok,$value) = $line =~ m/([A-Za-z0-9]+)_([A-Za-z0-9]+)/; if ( defined $value and defined $hash{$tok}) { print "Valid TOKEN/VALUE pairs: $tok $value\n"; } } =prints Valid TOKEN/VALUE pairs: K1 XXX Valid TOKEN/VALUE pairs: GEB ZZZ =cut __DATA__ 32 K1_XXX bbb _xxx ASC_ccc STUFF_YYY; M1_bbb; GEB2_NO GEB_ZZZ