use warnings; use strict; while () { chomp; if (/ ^ [cljso] \d{4} _ \d*? \. (dat|idx) $ /ix) { print "$_ : match\n"; } else { print "$_ : no match\n"; } } __DATA__ C1234ABC.IDX C1234_ABC.IDX C1234_.IDX