Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

substitute characters in the RHS of a search & replace

by Anonymous Monk
on Mar 08, 2013 at 21:30 UTC ( #1022499=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

From wikipedia, I have a string like the following: Here are [[a variable number of words]] in brackets.

I think I'm being dense, as I can't think of a one-liner I can use to substitute the double square brackets with their contents, while at the same time replacing spaces with underscores within the brackets. In other words, I want to get Here are a_variable_number_of_words in brackets.

Comment on substitute characters in the RHS of a search & replace
Select or Download Code
Replies are listed 'Best First'.
Re: substitute characters in the RHS of a search & replace
by kcott (Abbot) on Mar 08, 2013 at 21:56 UTC

    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

      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";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022499]
Approved by igelkott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2015-07-30 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls