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.
精彩评论