use strict; use warnings; my @flist = qw/MY.switch.Org.1st.txt router.2nd.Org.MY.txt 3rdswitchmyorg.txt/; for my $file (@flist) { $file =~ /(router|switch)/i or next; my $rs = uc $1; $file =~ /(\d+)(st|nd|rd)/i or next; my $pos = uc "$1$2"; print "$pos.$rs.MY.ORG.TXT\n"; }