my @boy_idxs; push @boy_idxs, $-[0] while $line =~ /\b boy \b/xg; substr($line, $boy_idxs[rand(@boy_idxs)], length('boy')) = 'man';