问题
I have around 3000 instance of replacement done in hundreds of files. Replacing all occurance of string1 with string2 was easy. IntelliJ allows me to replace all occurences in "comments and strings".
The problem is that the same string appear in comments and real code. I would like restrict the replacement only in comment section ( we use mix of /**/ or // )
Any library/IDE/script that can do this?
回答1:
use Regexp::Common 'comment';
...
s/($RE{comment}{'C++'})/(my $x = $1) =~ s#string1#string2#g; $x/ge;
回答2:
Try using the following regex to find all comments, and then replace what you want afterwards:
/(?>\/\*[^\*\/]*\*\/|\/\/([^\n])*\n)/
The first part \/\*[^\*\/]*\*\/
Tries to find all /**/
pairs where it finds something that starts with /*
and then contains something other than end tag */
and the contains end tag */.
THe other part checks something that starts with //
and goes to endline(\n
) and contains something not newline between ([^\n]*)
.
Thus it should all comments
来源:https://stackoverflow.com/questions/6538466/replace-string1-with-string2-in-many-java-files-only-in-comments