Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Regex pattern to find directories only with digits

by Bindo (Acolyte)
on Apr 11, 2013 at 09:55 UTC ( #1028109=perlquestion: print w/replies, xml ) Need Help??

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

Greetings good people! I just want to find all the directories containig only numbers and assign them to an array. For an instance I would want isolate the directories in a certain partition with this format "20130411". Not "2013.kjh" or "NN.201304", or "2013mmkk04". Following is what I tried but they returns not only what i want but the ones with letters

my @LOGDIR = <*>; foreach my $DIR(@LOGDIR) { if($DIR =~ m/([\d])/){ print "$DIR\n"; } }

Please can someone help. Many thanks in advance.


Replies are listed 'Best First'.
Re: Regex pattern to find directories only with digits
by hdb (Monsignor) on Apr 11, 2013 at 10:01 UTC

    This will also find files with names consisting of digits only...

    use strict; my @LOGDIR = <*>; foreach my $DIR(@LOGDIR) { if($DIR =~ m/^(\d+)$/){ print "$DIR\n"; } }

      Thank you very much sir this is exactly what I was looking for. :)

Re: Regex pattern to find directories only with digits
by reisinge (Hermit) on Apr 11, 2013 at 11:32 UTC

    In case you need to search recursively, you may use the File::Find core module:

    use strict; use warnings; use File::Find; # Search current dir unless dir(s) given as command line args @ARGV = qw(.) unless @ARGV; find( \&get_dirs_with_numbers, @ARGV ); sub get_dirs_with_numbers { return unless /^\d+$/; # your regex print $File::Find::name . "\n"; }

    find function from File::Find scans directories in @ARGV recursively and for each file calls the referenced function (coderef) get_dirs_with_numbers. Before calling your function find by default changes to the directory being scanned and sets the following variables:

    • $File::Find::dir -- visited directory path relative to the starting directory
    • $_ -- basename of the file being visited
    • $File::Find::name -- full path of the file being visited

    Well done is better than well said. -- Benjamin Franklin

Re: Regex pattern to find directories only with digits
by Krambambuli (Curate) on Apr 11, 2013 at 10:23 UTC
    with this format "20130411". Not "2013.kjh" or "NN.201304", or "2013mmkk04".

    You might need to refine your definition of 'this format'. Must it be a valid date ? Can it be in the future ? Can it be anywhere in the past ? Could it be less than 8 digits ? ... Etc. If you'd want _precisely_ 8 digits and wouldn't care about validness of the date,

    m/^[\d]{8}$/ might be the regex you want,



      Thank you for your thoughts Mr K. Well I just want to find any directory whos names are only made up of digits. It could either be a date, or even just a of a one digit. like 2. Thank you for clarifying doubts so sooner. :)

Re: Regex pattern to find directories only with digits
by tobias_hofer (Friar) on Apr 11, 2013 at 10:26 UTC


    I also recommend that you specify your regex as close to what you expect as possible. Just taking the "Class" for decimal is opening a wide range of possible matches. You may can start by defining how many digits you expect - mean to say giving your regex a Range. ;-)


Re: Regex pattern to find directories only with digits
by graff (Chancellor) on Apr 12, 2013 at 03:03 UTC
    If I were trying to get just the entries in my current working directory whose names consist entirely of digits, I'd do it like this:
    my @digit_dirs = grep /^\d+$/, <*>; print join( "\n", @digit_dirs, "" );
    If the entries of interest were in fact all "yyyymmdd" digit strings, I'd replace the "+" in the grep regex with {8}.

      Thank you very much good people. I totally understood the concept. You all are the best. By the way if I get a satisfactory answer to a thread that I have started is there a way of closing/locking it? Cos I receive lot of reminders from root saying that I'd have to reply ...etc. Im just a beginner gentlemen so please let me know if there are any rules that I should follow. thanks

        If you are on unix or similar shell, use:
        ls|grep -P "^.*\d{8}.*"
        -P in grep is for using perl regex within shell grep.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028109]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2023-01-28 19:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found