replace string1 with string2 in many java files, only in comments

混江龙づ霸主 提交于 2019-12-24 00:05:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!