foreach (@x=split(/(\.\s+)/, $text)) { s/^.*(\bnice\b.*)$/A $1/; } $text=join("", @x);