Why not just do something like:
perl -pi -e 's|echo (\".*?\");|custom_echo($1);|g' file.php
I don't think \Q
and \E
are doing what you think they're doing. They're not beginning and end of quotes. They're in case you put in a special regex character (like .
) -- if you surround it by \Q ... \E
then the special regex character doesn't get interpreted.
In other words, your regular expression is trying to match the literal string (.?*)
, which you probably don't have, and thus substitutions don't get made.
You also had your ?
and *
backwards -- I assume you want to match non-greedily, in which case you need to put the ?
as a non-greedy modifier to the .*
characters.
Edit: I also strongly suggest doing:
perl -pi.bak -e ... file.php
This will create a "backup" file that the original file gets copied to. In my above example, it'll create a file named file.php.bak
that contains the original, pre-substitution contents. This is incredibly useful during testing until you're certain that you've built your regex properly. Hell, disk is cheap, I'd suggest always using the -pi.bak
command-line operator.