Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Search and replace

by Athanasius (Chancellor)
on Jan 31, 2013 at 15:28 UTC ( #1016340=note: print w/replies, xml ) Need Help??

in reply to Search and replace

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,

Replies are listed 'Best First'.
Re^2: Search and replace
by Bharath666 (Novice) on Jan 31, 2013 at 15:53 UTC

    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;


        I thought I already had. :-) But OK, let’s add an /x modifier to the regex to make it easier to break apart:

        $found =~ s/ ENV{\" # match the characters: ENV{" (.*?) # followed by any characters -- this sequence +is the "value", and the parentheses capture it into the special varia +ble $1 \"} # followed by: "} / \$ENV( # and substitute a literal: $ENV(" $1 # followed by the captured "value" \") # followed by: ") /gx; # and repeat the search & replacement to the e +nd of $found


        • The double quotes are backslashed only because I was using a one-liner (and I’m on Windows). This is unnecessary in a .pl script. (But the $ must be backslashed to show that it is literal, not the special symbol for “match the end of a line” (in the search part) or a sigil denoting a scalar variable to be interpolated (in the replacement part). Update: Added the words from “(in the search part)” to the end of the sentence.)

        • The ? in the capture group (.*?) makes the match non-greedy. So the regex engine looks for the fewest number of characters occurring between ENV{" and "}. Without the ?, the match would be greedy, and on the string:


          it would match

        • which is not what we want!

        Hope that helps,

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016340]
[Corion]: Mhhmmm - https://sod.pixlab .io/ looks really interesting for embedding with Perl (XS), but they don't have any kind of free model available and the cheapest pretrained model costs EUR 40 :-(
[Corion]: Maybe I should mail them to find out if they can provide me a "hotdog / no hotdog" model for developping the XS bindings. It would be nice to have a self-contained XS library for applying models to data. Or maybe I should look at TensorFlow, which can...
[Corion]: ... at least be trained by me, instead of relying on a vendor
[Discipulus]: complex and interesting

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2018-06-18 10:46 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.