http://www.perlmonks.org?node_id=11112430

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I need to extract the zip code from a strange file path,I have a solution, just want to ask if there is a better way of doing this. here is the sample code to show my version of the regular expression I am using:
#!/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";
Thanks for looking!