2012-09-16 33 views
0

我試圖在perl中使用以下代碼,但它似乎沒有工作。如何匹配確切的sring而不插入任何字符串字符?我試着報價和\ Q,。\ E但沒有任何工程perl代碼中的確切字符串

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

爲清楚起見,字符串我想匹配是

`date +%s` 

哦,忘了提,date +%s是一個變量。

回答

3

必須濫用本\Q .. \E符,因爲這是你想要什麼

我認爲,從你說的話,你有`date +%s`,和反引號已經被降價吃掉了

在這種情況下,這段代碼會做你想做的。可變插值完成第一,之前和特殊字符

use strict; 
use warnings; 

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

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

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

print $string; 

輸出

xxx anotherthinghere yyy 
+0

我做錯了什麼是逃避\ Q和。\ E內部的反引號的解釋。我刪除它們,它的工作。謝謝。 – MinaHany

2

如果我也明白你的問題,怎麼樣:

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

輸出:

foo another bar