Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: substitute characters in the RHS of a search & replace

by kcott (Archbishop)
on Mar 08, 2013 at 21:56 UTC ( [id://1022502]=note: print w/replies, xml ) Need Help??


in reply to substitute characters in the RHS of a search & replace

Here's a solution that does what you're after. The substitution is:

s{\[\[(.*?)\]\]}{$1 =~ s/ /_/gr}e

I've added an additional test to check that various embedded brackets do not cause issues.

$ perl -Mstrict -Mwarnings -E ' my $x = q{Here are [[a variable number of words]] in brackets}; say $x; $x =~ s{\[\[(.*?)\]\]}{$1 =~ s/ /_/gr}e; say $x; my $y = q{Here are [[a variab]le number [ of ] wo[rds]] in brackets}; say $y; $y =~ s{\[\[(.*?)\]\]}{$1 =~ s/ /_/gr}e; say $y; ' Here are [[a variable number of words]] in brackets Here are a_variable_number_of_words in brackets Here are [[a variab]le number [ of ] wo[rds]] in brackets Here are a_variab]le_number_[_of_]_wo[rds in brackets

-- Ken

Replies are listed 'Best First'.
Re^2: substitute characters in the RHS of a search & replace
by Anonymous Monk on Mar 08, 2013 at 23:10 UTC

    Ah - I forgot you could do s/// on $1 - 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.

      > 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";
        ... a generic function to simulate /r ...

        Defining functions that take 'naked' subroutine blocks is, IMHO, one of the few valid reasons for using prototypes. However, there's a subtle pitfall here that belies the word 'generic' in the description of the function. The use of the  $ prototype causes behavior that will almost certainly cause puzzlement in a wider, i.e., more generic, context by imposing scalar context on its argument. Better, I think, to use  @ instead.

        >perl -wMstrict -le "sub rx (&$) { my $c_regex = shift; local $_ = shift; $c_regex->(); return $_; } ;; my $s = 'a b c'; print rx { s/ /_/g } $s; ;; my @ra = 'p q r'; print rx { s/ /_/g } @ra; " a_b_c 1 >perl -wMstrict -le "sub rx (&@) { my $c_regex = shift; local $_ = shift; $c_regex->(); return $_; } ;; my $s = 'a b c'; print rx { s/ /_/g } $s; ;; my @ra = 'p q r'; print rx { s/ /_/g } @ra; " a_b_c p_q_r

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1022502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found