2010-07-19 196 views
1

我有一個關於Perl的問題。Perl正則表達式用正則表達式的子串替換字符串

假設我在文件中的以下行:

DoMatchLong ("ThisIsMyVariable", lThisIsMyVariable); 

我想cp_const_ThisIsMyVariable

取代 「ThisIsMyVariable」

所以我的目標是:

DoMatchLong ( cp_const_ThisIsMyVariable, lThisIsMyVariable); 


$count = s/(?<=")\w+(?=")/const_cmd_cp_$&/gx; 

導致DoMatchLong ( "cp_const_ThisIsMyVariable", lThisIsMyVariable);

那麼正確的解決方案是什麼?

回答

5
$count = s/"(\w+)"/const_cmd_cp_$1/gx; 

將引號拉入匹配中,然後使用捕獲組來獲取引號之間的實際文本,同時仍然替換它們。

+1

非常感謝!你的回答比我理解你的解決方案要快。我喜歡分組正則表達式的可能性! – Menn 2010-07-19 06:55:50

+5

如果您嘗試瞭解決方案並且適用於您,您可能應該將問題標記爲已回答 – masonk 2010-07-19 12:46:59