How do I write a regular expression to find all not commented out System.out

时间:2015-07-28 16:24:19

标签: regex

In this project of ours the previous developers had coded throughout System.out even though told to only use the log.[debug,error etc.].

Been trying to write a RE to find all of the System.out that are not commented out but not having much luck. I did find here in the forumns information on negative lookbehind which sounds like what I need but I'm either getting all System.out including the commented ones or nothing at all in my efforts.

Appreciate any guidance on this. Here it the last regular expression I tried ((?<!\\)(?<!\s*))system.out

1 个答案:

答案 0 :(得分:0)

^ and $ match the beginning and the end of the line in Java. Also \ has to be escaped in Java Strings.

This is not perfect, but gives an idea, it tries to find System.out at the beginning of a line and then matches everything until ; over multiple lines if necessary:

Pattern pattern = Pattern.compile("^[ \\t]*System\\.out[^;]+");
// or "^[ \\t]*System\\.out\\.print(?:ln)?\\(.*?\\);"

Matcher matcher = pattern.matcher(...);
while (matcher.find()) {
  System.out.println(matcher.group());
}