开发者

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

开发者 https://www.devze.com 2023-01-08 01:52 出处:网络
I have a question concerning Perl. Assume I have the following line in my file: DoMatchLong ( \"ThisIsMyVariable\", lThisIsMyVariable);

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

精彩评论

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