开发者

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

开发者 https://www.devze.com 2023-03-16 18:29 出处:网络
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\".

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 st开发者_JS百科ring 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?


use Regexp::Common 'comment';
...
s/($RE{comment}{'C++'})/(my $x = $1) =~ s#string1#string2#g; $x/ge;


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

0

精彩评论

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

关注公众号