Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Using regex in file glob ?

by Wiggins (Hermit)
on May 03, 2021 at 13:19 UTC ( #11131975=perlquestion: print w/replies, xml ) Need Help??

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

Some times things are so obvious that they are not documented. But who can tell?

I am looking for a file in a directory using the 'glob' <>. within that glob is a non-simple RE.

my @possible = <${pathHi}(.*[_-]?RTM[_-]).*[.]xlsx>; #perl glob
This throws no errors, but never returns anything either. I am looking for something like:
RTM.xlsx RTM-v3.3_.xlsx system_2_RTM_v3-4.xlsx system 3-RTM.xlsx #yes, embeded spaces in file names

As I searched, the only example was '*', and no description of what could be used, or what internals would be doing the processing.

Pointers to some docs?

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re: Using regex in file glob ?
by Lotus1 (Vicar) on May 03, 2021 at 13:50 UTC

    File::Glob gives you a list of the metacharacters. glob() doesn't do regular expressions. It uses wildcard characters in the metacharacter list similar to ls or dir from the command line.

Re: Using regex in file glob ?
by Fletch (Chancellor) on May 03, 2021 at 17:36 UTC

    Perhaps Path::Tiny where the children method will take a regexp (sample from an OS X box):

    $ perl -MPath::Tiny=path -E 'say join( qq{\n}, path(q{/})->children(qr +/i[bn]|sr/) )' /usr /bin /sbin /Library /com.apple.TimeMachine.localsnapshots

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Using regex in file glob ?
by tybalt89 (Prior) on May 03, 2021 at 20:26 UTC

    A simple way...

    my @possible = grep /${pathHi}(.*[_-]?RTM[_-]).*[.]xlsx/, <*>;
Re: Using regex in file glob ?
by LanX (Cardinal) on May 03, 2021 at 13:52 UTC
    File-globs don't do regexes but wildcards like shell does.

    And I'm sure it's documented in glob

    If you want to apply regexes you need to work with opendir and check file by file, like inside a grep

    File::Find is also quite popular here.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11131975]
Approved by 1nickt
Front-paged by Corion
help
Chatterbox?
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: (4)
As of 2021-05-11 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (113 votes). Check out past polls.

    Notices?