http://qs321.pair.com?node_id=1107301


in reply to replace a string multiple times

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;
gives:
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;
outputs:
hint intintint stint