Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to pass regular expression as variable ?

by bh_perl (Monk)
on Apr 29, 2011 at 10:46 UTC ( [id://901955]=perlquestion: print w/replies, xml ) Need Help??

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


Hi..

Could somebody guide me how could i pass regular expression as variable ?. Below is mo code and i have test but it not works.

The wildcard value will be NX* or NX*xml or NX???.dat and etc. Please help me
if (defined($wildcard) && ($name =~ /$wildcard/)) { $ftp->get("$remotedir/$name","$localdir/$tmp") or $err +=1; if ($err) { print (LOGS "Can't collect file $wildcard from + dir $remotedir\n"); $ftp->quit; last; } else { rename ("$localdir/$tmp","$localdir/$fname"); } }

Replies are listed 'Best First'.
Re: How to pass regular expression as variable ?
by Eliya (Vicar) on Apr 29, 2011 at 11:01 UTC

    Maybe you're confusing regular expressions and shell wildcards?

    For example, NX* - as a regex - means N followed by zero or more X, which might not be what you intended.  Try .* in place of *

    See perlre.  And maybe also Regexp::Wildcards, in case you rather want to specify the patterns as wildcards.  Or Text::Glob, to directly match wildcard patterns against a string.

Re: How to pass regular expression as variable ?
by JavaFan (Canon) on Apr 29, 2011 at 11:01 UTC
    You can use pass around strings: if you use them as a pattern, it will be a pattern (in that sense, regular expressions aren't different from numbers or strings). This is legal:
    if ($var =~ "pattern") { .. }
    Or you could use a qr construct. See man perlop for details.
Re: How to pass regular expression as variable ?
by Anonymous Monk on Apr 29, 2011 at 13:11 UTC
    See "Regexp Quote-Like Operators" in perldoc perlop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-25 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found