#!/usr/bin/perl use strict; use warnings; print "\n"; my $file_1 = '-/yf/-/22211_01_09_2000_XYz.pdf'; # Just to show that the "hyphen" can be in any place in the path. my $file_2 = '_/gt/-/02239_04_04_1989_PkW.pdf'; my $file_3 = '-/xy/-/02239_04_04_1989_PkW.pdf'; my ($zip) = $file_1 =~ /^[\w\-]+\/[\w\-]+\/[\w\-]+\/(\d{5})_/; print "\n\n *$zip* \n\n\n";