Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is there a nicer way to do regex search and replace with literal backslashes?
by davido (Cardinal) on Apr 14, 2012 at 21:51 UTC | |
by AnomalousMonk (Archbishop) on Apr 15, 2012 at 01:09 UTC | |
by davido (Cardinal) on Apr 15, 2012 at 01:22 UTC | |
|
Re: Is there a nicer way to do regex search and replace with literal backslashes?
by moritz (Cardinal) on Apr 14, 2012 at 21:19 UTC |