Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 (Chancellor) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-01-20 10:23 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.