#!/usr/bin/perl -l for (qw(OTHFIL.FOO BARFIL)) { my ($foo) = grep defined($_), /OTHFIL\.([A-Z]{3})|([A-Z]{3})FIL/; print $foo; } __END__ FOO BAR