in reply to Re^2: substitute characters in the RHS of a search & replace
in thread substitute characters in the RHS of a search & replace
> I guess the /r switch allows you to do it as it doesn't modify $1. Thanks, good call. I'll check it's supported on my version of perl.
If not try this instead:
my $x = q{Here are [[a variable]] number [[of words]] in brackets}; say $x; $x =~ s{\[\[(.*?)\]\]}{ (my $x=$1) =~ s/ /_/g; $x }ge; say $x;
added a /g to allow multiple chunks to be processed.
Here are [[a variable]] number [[of words]] in brackets Here are a_variable number of_words in brackets
But to improve readability I would rather opt against one-liner and call a function in the replacement part
sub blank2under { my $x=shift; $x =~ s/ /_/g; return $x; }
Cheers Rolf
update
and here a generic function to simulate /r
sub rx (&$) { my $c_regex=shift; local $_=shift; $c_regex->(); return $_ } my $x = q{Here are [[a variable]] number [[of words]] in brackets}; print "$x\n"; $x =~ s(\[\[(.*?)\]\])( rx {s/ /_/g} $1 )ge; print "$x\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: substitute characters in the RHS of a search & replace
by AnomalousMonk (Archbishop) on Mar 09, 2013 at 19:35 UTC | |
by LanX (Saint) on Mar 09, 2013 at 20:07 UTC | |
by AnomalousMonk (Archbishop) on Mar 09, 2013 at 21:04 UTC |
In Section
Seekers of Perl Wisdom