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

Hi,
I have a problem using regular expresions and the glob operator. I want to get files with a specific name format from a directory using glob. The regular expresion I use to match the name format is

/[A-Za-z]{2}_\w{2,5}/ I can't get it to match the 2 to 5 \w.
Any suggestions on how to get this regular expression into glob much appreciated.

thanks
Alison

20031009 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: glob and regex
by broquaint (Abbot) on Oct 09, 2003 at 14:44 UTC
    Just look in the directory and apply grep e.g
    opendir(my $dir, 'your_directory_here') or die("ack: $!"); my @files = grep /[A-Za-z]{2}_\w{2,5}/, readdir $dir; closedir $dir;
    See. opendir and readdir for more info.
    HTH

    _________
    broquaint

      The grep should probably check if it's a file, not a directory:

      my @files = grep { -f && /[A-Za-z]{2}_\w{2,5}/ }, readdir $dir;

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

Re: glob and regex
by dws (Chancellor) on Oct 09, 2003 at 15:04 UTC
    I want to get files with a specific name format from a directory using glob.

    Adding to the recommendation to use opendir/readdir, I'm wondering if by "specific" name format you meant filenames that include the pattern you've given us, or filenames that are matched in their entirety by that pattern. If the latter, then you'll need to add anchors, as follows:

    /^[A-Za-z]{2}_\w{2,5}$/ ^ ^
Re: glob and regex
by Abigail-II (Bishop) on Oct 09, 2003 at 14:42 UTC
    It's not clear what you are actually doing. You have a regex, but you are also talking about globs. What are you doing with the glob? What are you matching against what, and what matches and what doesn't? /[A-Za-z]{2}_\w{2,5}/ matches any string that contains two letters (either case), followed by an underscore, followed by at least 2 word characters.

    But that's a regex, not a glob.

    Abigail

      According to Camel (3rd) on p. 85, it says:
      @files = glob $some_pattern; # Call glob as an operator.

      When I first read that, I thought $some_pattern meant $some_regex. But, it doesn't. (Or, rather, it does mean $some_regex, but it means to use a regex with a different syntax than Perl's regex syntax.)

      What the OP is looking for probably falls under the category of:

      my @files = grep { /[A-Za-z]{2}_\w{2,5}/ } glob "??_??";

      Two different regex syntaxes. *shrugs*

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: glob and regex
by Anonymous Monk on Oct 09, 2003 at 16:43 UTC
    Thanks folks! This is really helpful. I never thought of just using grep! Alison