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

replace a string multiple times

by amar712 (Initiate)
on Nov 15, 2014 at 07:54 UTC ( #1107282=perlquestion: print w/replies, xml ) Need Help??

amar712 has asked for the wisdom of the Perl Monks concerning the following question:

I would like to replace "int" with "int int int" in a file The below code is not working. perl -p -i -e "s/int/int{3}/g";

Replies are listed 'Best First'.
Re: replace a string multiple times
by aitap (Curate) on Nov 15, 2014 at 08:10 UTC
    The {3} you tried to use is for the pattern, not for the replacement. If you want to build the replacement string "on the fly", use the /e modifier (evaluate code) and build the string using ordinary Perl expressions: perl -p -i -e "s/int/join q[ ],(q[int])x3/ge", or just s/int/int int int/g.
Re: replace a string multiple times
by choroba (Archbishop) on Nov 15, 2014 at 08:12 UTC
    The quantifiers (like {3}) make sense in regular expressions. You used one in the replacement part, though, which is a pure string (and even if it worked, where would the spaces be?). There is no easy mechanism how to introduce repetition into a string, you have to call a function:
    s/int/ join ' ', ('int') x 3 /ge

    YMMV, but I think plain s/int/int int int/g is clearer.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: replace a string multiple times
by Loops (Curate) on Nov 15, 2014 at 11:25 UTC

    One way to do this is by putting what you're trying to repeat inside parentheses, and then use the $1 variable which will contain the matching contents:

    $_ = "hint int stint"; s/(int)/$1$1$1/g; print;
    hintintint intintint stintintint

    The $& variable contains the contents of your entire match without using the parentheses even:

    $_ = "hint int stint"; s/int/$&$&$&/g; print;

    And prints the same thing but can have performance issues if you're using a version of Perl prior to version 5.20.

    And in either case above, if you only want to match the entire word "int" and not when it's part of another word, use \b to designate a word boundary:

    $_ = "hint int stint"; s/\b(int)\b/$1$1$1/g; print;
    hint intintint stint
Re: replace a string multiple times
by AnomalousMonk (Bishop) on Nov 15, 2014 at 09:31 UTC

    Using join as others have suggested can give you better control of "inter-string separation", if you know what I mean, but you can do without it (and even without the  /e modifier):

    c:\@Work\Perl>perl -wMstrict -le "my $s = 'int ant aunt'; print qq{'$s'}; ;; my $n = 3; $s =~ s{ (int) }{ qq{$1 } x $n }xmsge; print qq{'$s'}; ;; $s =~ s{ (ant \s+) }{$1$1}xmsg; print qq{'$s'}; " 'int ant aunt' 'int int int ant aunt' 'int int int ant ant aunt'

Re: replace a string multiple times
by Anonymous Monk on Nov 15, 2014 at 08:11 UTC
    The string "int int int" does not appear anywhere in your program
Re: replace a string multiple times
by igelkott (Priest) on Nov 16, 2014 at 09:58 UTC

    You wrote "string" in the title and your question includes a quoted string but still, asking for "int" makes me wonder if you really meant that you're looking for integers. Seems unlikely, but here's a solution, just in case:

    s/(\d+)/$1 $1 $1/g;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2020-10-30 05:58 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (278 votes). Check out past polls.