use strict; use warnings; use feature 'say'; my $s = "cat hat bat"; # /g replaces every match; ${1} is the capture $s =~ s/(\w)at/${1}og/g; say $s; # cog hog bog
s///g with a capture group