perl替换中的确切字符串

时间:2012-09-16 10:19:14

标签: regex string perl match

我正在尝试在perl中使用以下内容但它似乎不起作用。如何在不插入任何字符串字符的情况下匹配精确的sring?我试过引号和\ Q,\ E但没有任何作用

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

为清楚起见,我想要匹配的字符串是

`date +%s`

哦,忘了提及date +%s在变量中。

2 个答案:

答案 0 :(得分:3)

您必须误用\Q .. \E说明符,因为这正是您想要的

我认为,根据你的说法,你有`date +%s`,并且反叛已被降价吃掉了

在这种情况下,此代码将执行您想要的操作。变量插值在 first ,特殊字符的解释和

之前完成
use strict;
use warnings;

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

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

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

print $string;

<强>输出

xxx anotherthinghere yyy

答案 1 :(得分:2)

如果我理解你的问题,那该怎么办:

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

<强>输出:

foo another bar