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

I'm trying to convert a shell wildcard into a valid regular expression for the purpose of creating a quick-n-dirty fileglob expander (using opendir and simple discard-if-not-match). Using File::KGlob is not really an option.. Does anyone have a ready recipe for this?
  • Comment on Converting shell wildcards into regular expressions

Replies are listed 'Best First'.
RE: Converting shell wildcards into regular expressions
by mrmick (Curate) on Aug 04, 2000 at 17:40 UTC
    Here's something from the Perl Cookbook. It can allow users to specify matches using shell wildcards instead of full regex's.
    sub glob2pat{ my $globstr = shift; my %patmap = ( '*' => '.*', '?' => '.', '[' => '[', ']' => ']', ); $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge; return '^' . $globstr . '$'; }
    I'm hoping that this can be tailored to whatever you need.

    Mick
Re: Converting shell wildcards into regular expressions
by tye (Sage) on Aug 04, 2000 at 19:34 UTC

    But that is exactly what File::KGlob does. I'm guessing you say using it isn't an option because you can't rely on it being installed. It is one, self-contained file and it isn't even very big. Just copy it to where you need it. It beats copying your own reinvented wheel to where you need it.