Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Search and replace

by Bharath666 (Novice)
on Jan 31, 2013 at 15:14 UTC ( [id://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

Replies are listed 'Best First'.
Re: Search and replace
by Athanasius (Archbishop) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-19 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found