I'm really getting my butt kicked here. I can not figure out how to write a search and replace that will properly find this string.
String:
$QData{"OrigFrom"} $Text{"wrote"}:
Note: That is the actual STRING. Those are NOT variables. I didn't write it.
I need to replace that string with nothing. I've tried escaping the $, {, and }. I've tried all kinds of combinations but it just can't get it right.
Someone out there feel like taking a stab at it?
Thanks!
No one likes quotemeta ? Let Perl figure it out so you don't strain you eyes with all those backslashes. :)
my $string = 'abc $QData{"OrigFrom"} $Text{"wrote"}: def';
my $escaped = quotemeta '$QData{"OrigFrom"} $Text{"wrote"}:';
$string =~ s/$escaped/Ponies!/;
print $string;
I originally thought that wrapping your regex in \\Q
/ \\E
(the quotemeta start and end escapes) would be all that you needed to do, but it turns out that $
(and @
) are not allowed inside \\Q...\\E
sequences (see http://search.cpan.org/perldoc/perlre#Escape_sequences ).
So what you need to do is escape the $
characters separately, but you can wrap everything else in \\Q ... \\E
:
/\$\QQData{"OrigFrom"} \E\$\QText{"wrote"}:\E/
regex using escape character \\ would be
s/\\$QData\\{"OrigFrom"\\} \\$Text\\{"wrote"\\}://;
full test code:
#!/sw/bin/perl
$_='$QData{"OrigFrom"} $Text{"wrote"}:';
s/\$QData\{"OrigFrom"\} \$Text\{"wrote"\}://;
print $_."\n";
outputs nothing but newline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.