my $regex = "foo"; #any pattern, few characters here for simplicity my $string = "this is a foo"; $string =~ s/$regex/'bar'/ge; print $string;