use strict; use warnings; my $filename = "p_abc_xyz_123.txt"; print "$1$2$3\n" while( $filename =~ s/(.*)(_\w{3})(\..*)/$1$3/ );