开发者

Perl regular expression replace string with a substring of the regular expression

I have a question concerning Perl.

Assume I have the following line in my file:

DoMatchLong ( "ThisIsMyVariable", lThisIsMyVariable);

I want to replace "ThisIsMyVariable" by cp_const_ThisIsMyVariable

So my aim is:

DoMatchLong (  cp_const_ThisIsMyVariable, lThisIsMyVariable);


$cou开发者_Python百科nt = s/(?<=")\w+(?=")/const_cmd_cp_$&/gx;

leads to DoMatchLong ( "cp_const_ThisIsMyVariable", lThisIsMyVariable);

So what is the correct solution?


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

Pull the quotes into the match, then use a capturing group to get only the actual text between the quotes, while still replacing them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜