Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Turning an argument into a regexp

by LoneRanger (Acolyte)
on Aug 30, 2000 at 22:00 UTC ( #30359=perlquestion: print w/replies, xml ) Need Help??

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

This code snippet is part of a playlist generator. I'd like to be able to take in arguments such as dir. to start searching at, and what to search for. The File::Find mod. seems to have some probs with the regexp being a variable...Any solutions?
my $MP3dir = $ARGV[0]; my $searchfor = $ARGV[1]; #$MP3dir = "\"" . $MP3dir . "\""; my $regexp = "\/\\.$searchfor\/"; find(\&wanted, $MP3dir); sub wanted { $regexp or return;

Replies are listed 'Best First'.
(jcwren) Re: Turning an argument into a regexp
by jcwren (Prior) on Aug 30, 2000 at 22:04 UTC
    It's because wanted isn't evaluating the regexp, it's merely treating it as a string.

    Try removing the '\/' from the front and end of your $regexp string, then this code fragment.
    sub wanted { /$regexp/ or return; }
    Just for the record, you may want to use /$regexp/i to ignore case from the command line, and play both those .MP3 and .mp3 files. Also, this line: $MP3dir = "\"" . $MP3dir . "\""; can be reduced to $MP3dir = "\"$MP3dir\""; There are other ways to quote that, also. That's just a tad more readable.


    e-mail jcwren
Re: Turning an argument into a regexp
by le (Friar) on Aug 30, 2000 at 22:04 UTC
    I don't think the File::Find module has problems with a variable being a regexp. But you should let Perl know that you actually mean a regexp. In your example, $regex is treated as a scalar, not as a regex. Try something like this:
    my $regex = "\\.$searchfor"; $file =~ m/$regex/;
    Putting the regex delimiters into the variable may work with an eval, but this is just a wild guess.
Re: Turning an argument into a regexp
by ncw (Friar) on Aug 31, 2000 at 01:16 UTC
    I agree with the comments above. I'd just like to add an oft forgotten thing (obscure feature alert ;-) to do with regexps, namely \Q and \E for turning off regular expression parsing.

    If $searchfor contained characters special to regexp (eg '.' or '?') then you won't get quite the results you expect, and your program may crash with an invalid regexp error. Use \Q and \E something like this...

    my $MP3dir = $ARGV[0]; my $searchfor = $ARGV[1]; find(\&wanted, $MP3dir); sub wanted { /\.\Q$searchfor\E$/ or return
RE: Turning an argument into a regexp
by KM (Priest) on Aug 30, 2000 at 22:10 UTC
    Look at qr/STRING/ (perdoc perlop).

    my $re = /\d+$/; # for an example pattern $thingy =~ /My Zip code is $re/; $thingy =~ /$re/; $thingy =~ s/foo{$re}bar/baz/; etc...


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://30359]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2023-01-31 06:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found