Perl正则表达式用正则表达式的子字符串替换字符串

时间:2010-07-19 06:48:12

标签: regex perl substring

我对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);

那么正确的解决方案是什么?

1 个答案:

答案 0 :(得分:5)

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

将引号拉入匹配,然后使用捕获组仅获取引号之间的实际文本,同时仍然替换它们。

相关问题