Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Search and replace

by Bharath666 (Novice)
on Jan 31, 2013 at 15:14 UTC ( #1016335=perlquestion: print w/replies, xml ) Need Help??
Bharath666 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I want to replace all occurrences of ENV{"value"} in the following string with $ENV("value"). This "value" is for example and varies.


please help me how to do this

Replies are listed 'Best First'.
Re: Search and replace
by Athanasius (Canon) on Jan 31, 2013 at 15:28 UTC

    Use the substitution operator s///g like this:

    1:30 >perl -wE "my $found = 'ENV{\"VCINSTALLDIR\"}\ATLMFC\LIB\amd64;E +NV{\"LIBPATH\"}'; $found =~ s/ENV{\"(.*?)\"}/\$ENV(\"$1\")/g; say $fo +und;" $ENV("VCINSTALLDIR")\ATLMFC\LIB\amd64;$ENV("LIBPATH") 1:30 >

    Within the substitution, the parentheses capture the “value” text which is used in the replacement as $1. The /g modifier tells the regex engine to keep searching and substituting until the string is exhausted.

    See perlretut and perlre.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you!! Really I am weak in handling regex of this kind. Thank you soooo much

        Also if you don't mind can you please explain me how exactly this command works

        $found =~ s/ENV{\"(.*?)\"}/\$ENV(\"$1\")/g;


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2016-09-29 19:37 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (560 votes). Check out past polls.