问题
Can someone share a regex that find all not double-slashed commented println in java code?
Example:
System.out.println("MATCH") /*this line match*/
// System.out.println("DOESN'T MATCH") /*this line doesn't match*/
(I'm using this regex into throw eclipse searching dialog)
回答1:
Okay, as I already mentioned, regex is not the right tool, so if you end up using my suggestion, be sure to backup your source!
The following regex matches a single line that has System.out.print in it, without // or /* before it (in that same line!).
(?m)^((?!//|/\*).)*System\.out\.print.*
or simply:
(?m)^[ \t]*System\.out\.print.*
which can then be replaced with:
//$0
to comment it.
Again: this will go wrong with multi line comments, and as Kobi mentioned, stuff like /* // */ System.out.print... to name just two of the many cases this regex will trip over.
Also consider the line:
System.out.println("..."); /*
comments
*/
you don't want to end up with:
//System.out.println("..."); /*
comments
*/
回答2:
You could probably just do something simple like:
^[ \t]*[^/][^/].*println.*
来源:https://stackoverflow.com/questions/5374843/regex-to-find-an-uncommented-println