Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Using a regex saved in an environment variable

by J0ax88 (Initiate)
on May 03, 2012 at 02:03 UTC ( #968605=perlquestion: print w/ replies, xml ) Need Help??
J0ax88 has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a regular expression stored in an environment variable. I have this one-liner that works fine without using an environment variable:

perl -00 -ne 'print if /--Begin--/ && /--End--/' file.txt

file.txt contains:

Hello World --Begin-- Cat and Dog --End-- other stuff This is a test Good Bye

The above one-liner prints out what I want:

--Begin-- Cat and Dog --End--

Now I want to do the same thing except put the regular expression in an environment variable:

export REX="/--Begin--/ && /--End--/" echo $REX /--Begin--/ && /--End--/

So I tried doing this:

perl -00 -ne 'BEGIN {$rex = $ENV{REX}} print if qr($rex)' file.txt

and also this:

perl -00 -ne 'BEGIN {$rex = qr($ENV{REX})} print if $rex' file.txt

But it always prints out the whole file. Any help would be appreciated.

Comment on Using a regex saved in an environment variable
Select or Download Code
Re: Using a regex saved in an environment variable
by bobf (Monsignor) on May 03, 2012 at 02:13 UTC

    I suspect $_ is not what you think it is in the print. Try to get it to work using explicit variables first (one for the file content, one for the regex, and one for the match), then you can watch what happens as you remove them.

    I don't mean to be cryptic, but I think you're on the right track here...

Re: Using a regex saved in an environment variable
by kcott (Abbot) on May 03, 2012 at 02:33 UTC

    Your problem here is that you're actually using two regexes. Interpolating these separately works fine:

    ken@ganymede: ~/tmp $ cat pm_re2_file.txt Hello World --Begin-- Cat and Dog --End-- other stuff This is a test Good Bye ken@ganymede: ~/tmp $ RE1="--Begin--" RE2="--End--" perl -00 -ne 'print if /$ENV{RE1}/ && +/$ENV{RE2}/' pm_re2_file.txt --Begin-- Cat and Dog --End-- ken@ganymede: ~/tmp $

    If you have a specific requirement to use just one environment variable, I'd suggest using eval:

    ken@ganymede: ~/tmp $ RE1="/--Begin--/ && /--End--/" perl -00 -ne 'print if eval $ENV{RE1} +' pm_re2_file.txt --Begin-- Cat and Dog --End-- ken@ganymede: ~/tmp $

    -- Ken

Re: Using a regex saved in an environment variable
by JavaFan (Canon) on May 03, 2012 at 08:28 UTC
    Now I want to do the same thing except put the regular expression in an environment variable:

    export REX="/--Begin--/ && /--End--/" echo $REX /--Begin--/ && /--End--/
    But that's not a regexp; that's an expression.

    You can do the interpolation in the shell, so that Perl sees what you intent it to see:

    perl -00 -ne 'print if '$REX file.txt
    Here, $REX is exposed to the shell, which sees the above as:
    perl -00 -ne 'print if /--Begin--/ && /--End--/' file.txt
Re: Using a regex saved in an environment variable
by J0ax88 (Initiate) on May 04, 2012 at 01:50 UTC

    Thanks Everyone. All the comments were very helpful. This works good:

    perl -00 -ne 'print if eval $ENV{REX}' file.txt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-08-31 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls