Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
XP is just a number
 
PerlMonks  

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.

$found="ENV{\"VCINSTALLDIR\"}\ATLMFC\LIB\amd64;ENV{\"LIBPATH\"}"

please help me how to do this

Comment on Search and replace
Download Code
Re: Search and replace
by Athanasius (Prior) 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;

        Thanks!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-04-17 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls