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

Hello All, Any ideas how to recreate a dos wildcard filemask of the typ +e "help*.txt" as a string for insertion in a pattern match. At the + moment I'm trying to write a subroutine that returns an escaped stri +ng which I then insert in my m// ... ie $myregex = &my_regex_generator("help*.txt"); if( $mystring =~ /$myregex/i){do stuff.... at the moment I've got - sub gen_convert_wildcards_to_regexp{ my $wildcardstring = $_[0]; my $regex_string = $wildcardstring; $regex_string =~ s/\./\\\./gi; #escape the dots $regex_string =~ s/\*/\\*/gi; #escape the $regex_string = $regex_string . "\\b\$"; return $regex_string; } Which is pretty crap and fails with the * wildcard
I can get my users to handle wildcards but regexp... no chance. Any suggestions are as always appreciated and perhaps wiser eyes will say that I'm tackling this the wrong way .... ? Thanks

Replies are listed 'Best First'.
Re: dos file masks and regular expressions
by reasonablekeith (Deacon) on Jan 04, 2006 at 11:24 UTC
    Presuming (in your example) "do stuff..." is going to go off and try to find the files, you can probably get away with using 'glob', which will do both steps in one and return a list of files matching the passed expression...
    my @sources = glob("c:\\temp\\*"); print join("\n", @sources) . "\n"; __OUTPUT__ c:\temp\afile.txt c:\temp\anotherfile.txt etc...
    ---
    my name's not Keith, and I'm not reasonable.
      Actually its a bit more complicated than that and perhaps I should have given more information. What I'm actually doing is trying to pass the generated regexp to archive::zip to create an archive according to a dos filemask which archive::zip doesn't seem to do. If I globbed the dir I'd then have to handle writing loads of individual members to my archive ....
Re: dos file masks and regular expressions
by pKai (Priest) on Jan 04, 2006 at 11:58 UTC

    DOS wildcards only know 2 meta symbols: * and ?.

    In regex speak these would be written as .* and . respectively *)

    All other characters have to be taken literally.


    Update: depending on how close you want to come to the results of DOS's "dir" you need additional restrictions, like surrounding the expression with begin and end assertions as in: $re = '^help.*\.txt$';

    Update: *) ? is exactly 1 character (not "one or none" as my previous .? described.
    Thanks to riceWind for spotting my error.

      I don't think you are correct about the ? wildcard. This is equivalent to plain '.' as zero occurrences of the character will not generate a match.

      For example ?ana*.txt will match banana.txt and panama.txt but not analyst.txt

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

      Great !! . . Thanks, that works a treat. Didn't know to put the dot in front ie .*
Re: dos file masks and regular expressions
by rinceWind (Monsignor) on Jan 04, 2006 at 12:34 UTC

    You might be interested in my module File::Wildcard which does what you want and more. Comments and feedback will be most welcome.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Thanks, I'll check that out.
Re: dos file masks and regular expressions
by Anonymous Monk on Dec 13, 2017 at 13:10 UTC
    This worked for me. $s_regx =~ s/\./\\./g; $s_regx =~ s/\*/.*/g;