s/\b\w+\s+nice\b/A nice/g; #### $w = 'nice'; $r = 'A'; s/\b\w+(\s+$w)\b/$r$1/g;