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

How can I use a RegEx within a glob ?

 my @EnvListFile=glob("$HOMEDIR/data/EnvList." . /\d{10}/);

All I see in my search thus far is "can't", or "filter after glob" Your help is much appreciated... Thanx

Replies are listed 'Best First'.
Re: RegEx within glob
by moritz (Cardinal) on Feb 22, 2012 at 14:54 UTC

    You're right, you can't combine globs and regexes, but of course you can filter the result of a glob based on a regex:

    my @EnvListFile = grep /EnvList\.\d{10}/, glob("$HOMEDIR/data/EnvList.*");
Re: RegEx within glob
by Eliya (Vicar) on Feb 22, 2012 at 15:02 UTC

    File globbing has its own pattern syntax and matching semantics, which aren't regex compatible (and much more limited in features).

    The closest you could get for your \d{10} example is the pattern [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].

        I wasn't suggesting you should write "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" in Perl — sure you can generate the pattern any way you like :)

        I just meant to point out that globbing itself provides no means to say "10 times" a subpattern (like {10} in regex); you have to specify the pattern "unrolled" instead.

Re: RegEx within glob
by Marshall (Canon) on Feb 22, 2012 at 14:55 UTC
    The basic answer is that you can't. glob() can be cool thing, but it's pattern matching capabilities are primitive.

    my @EnvListFile=glob("$HOMEDIR/data/EnvList.*");
    would work. If you want something more sophisticated than that, you need grep. regex can be used in a grep, but not a glob().
    open DIR, "$HOMEDIR/data/EnvList" or die $!; my @EnvListFile = grep{ -f "$HOMEDIR/data/EnvList/$_" and /\d+$/ } readdir DIR; #perhaps???
    moritz's answer is fine also.
Re: RegEx within glob
by Anonymous Monk on Feb 22, 2012 at 23:16 UTC

    use File::Find::Rule;

    my @EnvListFile = find( file => name => qr/EnvList\.\d{10}/, in => "$HOMEDIR/data" );