use strict; use warnings; my $needle = 'mo4\dy13\hr19\\'; #escape final backslash $needle =~ s/\\/\\\\/g; # escape all backslashes or they will mess up +the regex my $haystack = '\yr2012\mo4\dy13\hr19\min23\sec51'; if ($haystack =~ /$needle(.*?3)/) { print "found $1\n"; my $match = $1; my $replacement = 'new'; my $newneedle = $needle; $newneedle =~ s/\\\\/\\/g; # unescape the backslashes or there will +be too many in the final string $haystack =~ s/$needle$match/$newneedle$replacement/; # I want all v +ariables here, not literal strings } else { print "not found\n"; } print "$haystack\n";
In reply to Is there a nicer way to do regex search and replace with literal backslashes? by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |