#!/usr/bin/perl while() { chomp; if( /\A[\w]{3}_\w+_ear\Z/ ) { print "$_ matches \n"; } else { print "no joy for $_ \n"; } } __DATA__ ab_wehave_noear abc_test_ear abc_test_two_ear abcc_test_ear #### no joy for ab_wehave_noear abc_test_ear matches abc_test_two_ear matches no joy for abcc_test_ear