Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: regex substitution

by dws (Chancellor)
on Nov 05, 2001 at 08:35 UTC ( #123275=note: print w/replies, xml ) Need Help??

in reply to regex substitution

But I can't for the life of me find an example that does this (substitutions within substitutions).

When doing substitutions like this, /e is your friend.   $text =~ s/\#\w+\#/$hash{$1}/eg;
/e forces the right-hand side of the s/// to be evaluated as a Perl expression, and the result substituted for whatever the left-hand side matched. The boundary between interpolation and substitution can seem pretty hazy, but it makes a bit more sense if you imagine that the regexp is "compiled" immediately before it is executed. At that point, $1 either doesn't have a value, or holds a value from a prior regexp. Neither is what you want.

Consult perlre for full details.

Replies are listed 'Best First'.
Re: Re: regex substitution
by coolmichael (Deacon) on Nov 05, 2001 at 10:40 UTC
    Perhaps it shouldn't work without /e, but it does, at least with activestate perl 5.6.1. (obviously, you should use strict and warnings...)
    #!perl $_="this is a #fish#\n"; $a{"fish"}="test"; print; s/#(\w+)#/$a{$1}/; print; this is a #fish# this is a test C:\perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 626 provided by ActiveState Tool Corp. http://www.ActiveS Built 01:31:15 May 2 2001
      Have you read what the /e switch does? I sugguest you do.

      Your argument basically boils down to s/#(\w+)#/$variable/; in which $variable gets expanded, as it will no matter what (if /e is there or not).

      Compare this to:

      #!perl -w use strict; my %hashola = (fish => "test" ); sub f($){ return $hashola{shift @_}; } my $rock = "this is a #fish#\n"; my $block = $rock; print $rock; $block =~ s/#(\w+)#/&f($1);/; # expands $1, doesn't call &f print $block; $block = $rock; # restoree $block =~ s/#(\w+)#/$hashola{$1}/; # expands the variable print $block; $block = $rock; # restoree $block =~ s/#(\w+)#/&f($1)/e; # calls the function print $block; $block = $rock; # restoree $block =~ s/#(\w+)#/$hashola{$1}/e; print $block; $block = $rock; # restoree __END__ F:\dev>perl f this is a #fish# this is a &f(fish); this is a test this is a test this is a test F:\dev>
      Ok ok, from perlop (since s is an operator), I quote:
      e Evaluate the right side as an expression.
      Unless you escape $hashname{$1} like \$hashmane{$1}, the actual variable will be expanded independent of /e, as such is the nature of the s operator.

      Disclaimer: Don't blame. It came from inside the void

      perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://123275]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-03-23 21:01 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (296 votes). Check out past polls.