Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 (Abbot) 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 chanting in the Monastery: (4)
As of 2015-07-05 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls