DewinDell
DewinDell

Reputation: 1028

exact string in perl subtitute

I'm trying to use the following in perl but it doesn't seem to be working. How do I match the exact sring without any interpolation of any string characters? I tried quotes and \Q, \E but nothing works

$string=~ s/\`date +%s\`/anotherthinghere/;

for clarity, string I'm trying to match is

`date +%s`

Oh, forgot to mention that date +%s is in a variable.

Upvotes: 0

Views: 564

Answers (2)

Borodin
Borodin

Reputation: 126722

You must have misused the \Q .. \E specifiers, as that is exactly what you want

I think, from what you say, that you have `date +%s`, and the backticks have been eaten by the markdown

In that case, this code will do what you want. The variable interpolation is done first, before and interpretation of the special characters

use strict;
use warnings;

my $string = 'xxx `date +%s` yyy';

my $pattern = '`date +%s`';

$string =~ s/\Q$pattern/anotherthinghere/;

print $string;

output

xxx anotherthinghere yyy

Upvotes: 3

Toto
Toto

Reputation: 91385

If I well understand your question, how about:

my $var = q/`date +%s`/;
my $string = q/foo `date +%s` bar/;
$string =~ s/\Q$var/another/;
say $string;

output:

foo another bar

Upvotes: 2

Related Questions