Regex to find uncommented println

Can anyone share a regex that finds all non duplicated println comments in java code?

Example:

System.out.println("MATCH") /*this line match*/ // System.out.println("DOESN'T MATCH") /*this line doesn't match*/ 

(I use this regular expression in the eclipse search dialog box)

+4
source share
2 answers

Well, as I mentioned, regex isn't the right tool, so if you end up using my suggestion, be sure to back up your source!

The following regular expression matches the same line with System.out.print in it, without // or /* in front of it (on the same line!).

 (?m)^((?!//|/\*).)*System\.out\.print.* 

or simply:

 (?m)^[ \t]*System\.out\.print.* 

which can then be replaced by:

 //$0 

to comment on this.

Again: this will not be the case with multi-line comments, and as Kobe said, for example, as /* // */ System.out.print... to name only two of many cases, this regular expression will fire.

Also consider the line:

 System.out.println("..."); /* comments */ 

you do not want to end up with:

 //System.out.println("..."); /* comments */ 
+8
source

You could just do something simple:

 ^[ \t]*[^/][^/].*println.* 
-2
source

Source: https://habr.com/ru/post/1344562/


All Articles