regex to find an uncommented println

会有一股神秘感。 提交于 2019-12-23 13:56:52

问题


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

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