$_ = '*that old time* music *kinda soothes* my soul'; s{ # sub ( # capture in $1 (\B[^\w\s]\b) # capture token in $2 at boundary [^\2]+? # 1 or more non tokens (minimum of) \b\2\B # matching token at boundary ) # close capture $1 } # close the first part of the regex # now generate the substitution pattern { $fnd = $1; # store what we have found $tok = $2; # this is our token to add $fnd =~ s/ # now modify $fnd (\w) # find a letter -> $1 (\s+) # space(s) -> $2 (\w) # another letter-> $3 # now add the tokens we want /$1$tok$2$tok$3/xg; $fnd # $fnd will now get substituted into our string }gex; print;