#!/usr/bin/perl use warnings; use strict; use Syntax::Construct qw{ \K }; while () { s/ (?:clothes|something) # Starting keyword. \K # Don't include the previous part into the replacement. (.*?) # The part to be quoted. (?=\.m) # Followed by ".m". /'$1'/xg; print; } __DATA__ abc clothes doing shopping at the .m abc something doing shopping at the .mabc