2

I need to remove all System.out statements from my code on eclipse.

example:

System.out.println("hello");
System.out.println("hello" +a);
System.out.println(1 +b12);

basically all System.out statements no matter what their parameter is.

I have to the following but it doesn't even come close

System.out.println(.)*;?``

f1sh
  • 11,489
  • 3
  • 25
  • 51
124697
  • 22,097
  • 68
  • 188
  • 315
  • 1
    See [this](http://stackoverflow.com/a/7522068/2235132) answer. – devnull Sep 11 '13 at 08:05
  • 2
    Tricky. The statement may span multiple lines. The part to be printed can contain all kinds of stuff (think '"');"'). `System.out` may be called something else in some places. – Thilo Sep 11 '13 at 08:05
  • Risky... imagine you have this statement: if (boolean) System.out.println(""); Removing the System.out line will cause a change it the way your program executes, possibly without even a compile error showing up! – jimjim Sep 11 '13 at 08:11
  • 1
    @jimjim if you have lines like `if (boolean) System.out.println("");` you really need better coding conventions. – STT LCU Sep 11 '13 at 08:22
  • 1
    @stt-lcu I agree completely... but if you have a bunch of System.out.printlns in your code, I'm already thinking your conventions might be lax, so I wouldn't make any assumptions here! – jimjim Sep 11 '13 at 08:28
  • Also, it still is very valid Java. And it's not a solution if it only works when your code follows a certain style, it should work for all java code. – f1sh Sep 11 '13 at 08:40

9 Answers9

5

As an alternative, you could try to redirect System.out to somewhere else (or nowhere) using System.setOut(somePrintStream) at the top of your program.

If you are running in Eclipse, it will also let you specify where System.out should go instead of the normal console view (somewhere in Run Configurations).

And if this is to get rid of debug logging, consider replacing it not with nothing, but with calls to a logging framework. Then you can turn it on or off at runtime.

Thilo
  • 257,207
  • 101
  • 511
  • 656
3

you can use following regex and simply do find and replace. There is a option for regex find and replace in Eclipse.

System.out.println\(([^;].*\)*);

I tested for following

 System.out.println("  ");
 System.out.println("  fdfdf");
 System.out.println(")  fdfdf");
 System.out.println(";");
 System.out.println();

This may help you to find regex find and replace in Eclipse

Here is regex online test. I put some data there. any one can test with any thing.

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3

I would go the following (admittedly windy) route:

  1. Create a class my.domain.ReplaceHelper with a public static void helpMeToReplaceSysouts(Object obj) {}
  2. Search and replace System.out.println for my.domain.ReplaceHelper.helpMeToReplaceSysouts in the source code
  3. Refactor: inline the helpMeToReplaceSysouts method.
vitaly
  • 2,755
  • 4
  • 23
  • 35
2

Why can't you try this instead? It does the job as well!

In Eclipse, CTRL+F gives the Find/Replace window. In that give the below strings,

Find String - System.out.println Replace String - //System.out.println

Hurray, you commented out all the sysouts!

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Best solution :) (Beside multiline stuff - but this will result in syntax errors and can be fixed manually then.) – dognose Sep 11 '13 at 08:12
  • I would really not recommend it. You can change the logical flow of your program without realising. I have actually done this! – jimjim Sep 11 '13 at 08:13
  • unless and until you use `System.out.println` for anything other than what its supposed to do, I don't see any harm, why you shouldn't use this.(As @dognose said, multiline stuff is the only potential problem, which can be manually fixed, as eclipse is smart enough to show those **red** lines!). – Rahul Sep 11 '13 at 08:18
  • @jimjim how? did you have lines like `System.out.println("foo"); $this->callGodMethod();`? – STT LCU Sep 11 '13 at 08:20
  • 2
    @stt-lcu e.g. if (logout) System.out.println(""); a++; – jimjim Sep 11 '13 at 08:21
  • It's not a programmatic solution -1 – vladkras Sep 11 '13 at 08:23
  • 1
    @vladkras-OP never mentioned that a programmatic solution is only required! This is just a quick fix alternate, which would work in most cases, unless there are formatting issues in the code or if the `if` block statements are not bound within braces! – Rahul Sep 11 '13 at 08:25
  • @jimjim where are your curly braces? Never write an if without curly braces because it's dangerous (like in your example). Anyway, i see your point. – STT LCU Sep 11 '13 at 08:31
  • @stt-lcu yes you're right but sadly I've seen plenty of examples in the wild where this convention is not followed. – jimjim Sep 11 '13 at 08:38
1

This is hard for several reasons:

  • Java, as most languages, is a Type 2 (context-free) language in the Chomsky hierarchy. That means that there can be an arbitrarily complex statement in the println parameters, such as println(doA(4+(-1))+format(");"); that makes it extremely hard to find the end of the println statement using a regex. The only reliable approach is to parse the code according to it's type 2 attributes (as Brandon pointed out) or to leave it to a tool such as eclipse's refactoring methods (as R.J stated).
  • Following the regex approach, you might look for an start-of-line/end-of-line thing, such as ^\s*System\.out\.println\(.*);\s*$, but that will not catch multiline-println-statements
  • You won't find a regex to catch all the possible cases of what the parameter could be like.

Overall: Thilo's answer is the best choice. Although that means your code has to already be written accordingly.

Community
  • 1
  • 1
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • "Although that means your code has to already be written accordingly." Redirecting System.out somewhere should work reliably. – Thilo Sep 11 '13 at 08:41
  • I agree, sorry. I had your "calls to a logging framework" in mind, which is clearly the best choice. – f1sh Sep 11 '13 at 08:43
0
  1. You have to escape the parentheses
  2. The asterisk should follow the dot immediately to mean "any number of any characters"
  3. You should avoid the .* to catch your entire program, so use some delimiter that doesn't appear in your output-statements like for example ;

To sum it all up I would use something like

System.out.println\([^;]*\);
piet.t
  • 11,718
  • 21
  • 43
  • 52
0

The easiest would be to ignore the brackets. Only thing that IS Sure, is that the statement will end with ";" and most likely (depending on code style) with a new Line.

without line-delimiters, .*? has a good chance to match anything you would NOT match.

^System\.out\.println.*?;\s*$
dognose
  • 20,360
  • 9
  • 61
  • 107
  • ``println`` statements can span over multiple lines. It's ugly, but it does happen a lot. – f1sh Sep 11 '13 at 08:09
  • See my edit. If you have something like this, there is no save way to cover everything. – dognose Sep 11 '13 at 08:10
  • ..which is exactly why this problem cannot be solved elegantly using regular expressions. The problem is that using a regex-replace on code which contains multiline-``println``-statements leaves you with non-compilable code. – f1sh Sep 11 '13 at 08:38
0

Several of the other suggestions will get most of them, but they will mostly ignore several pedantic cases, such as SomeOtherSystem.out.println, or "this string contains a System.out.println()" or even "class System(){ public Someting out; ...}; System.out.println("this isn't what you think it is");

Ultimately, you can't 100% perfectly reason about Java with regular expressions alone. It sounds like you only want to remove them from your own code base this one time, so just use this and check up on the results.

If you wanted to really automate this properly (for instance, if you wanted to build a tool to do this), I'd suggest that you use a real Java parser, like this one: https://code.google.com/p/javaparser/

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
-2

Try this: System.out.println\(.*?\)

JSFiddle for those who doesn't believe (it's written in JavaSscript, not Java, but the same pattern will work everywhere)

EDIT This regexp: /System\.out\.println\((?<=\().*(?=\))\)/ removes all inside matching brackets: JSFiddle

TylerH
  • 20,799
  • 66
  • 75
  • 101
vladkras
  • 16,483
  • 4
  • 45
  • 55
  • `.*?` ... You will replace a good portion of your code with that one :D – dognose Sep 11 '13 at 08:05
  • @dognose: No. The question mark makes it non-greedy. So it will stop at the first closing parens. – Thilo Sep 11 '13 at 08:08
  • @hsz really? http://jsfiddle.net/vladkras/HgMCb/ and stop downvoting if you're not sure – vladkras Sep 11 '13 at 08:09
  • 3
    @vladkras Really: http://jsfiddle.net/HgMCb/1/ - However I didn't downvoted your answer. – hsz Sep 11 '13 at 08:11
  • @vladkras this doesn't work because you are matching ) inside the argument to println, potentially. – Sean Owen Sep 11 '13 at 08:13
  • as @hsz pointed out, this regex will match until the first closing parenthesis. If there's one in the parameters (such as a function call or in the string literal), this will not work. So the "stop downvoting if you're not sure" is quite cocky. – f1sh Sep 11 '13 at 08:13
  • I just answer to exact question of *codefish*. Why are you escaping `"` or putting another `(` inside? [This fiddle](http://jsfiddle.net/vladkras/HgMCb/2/) works. Where is the problem? You just answering your own questions in your own comments. – vladkras Sep 11 '13 at 08:20
  • 1
    If you are just having the original examples in mind, then you should change your approach of developing software. Expect the unexpected. – f1sh Sep 11 '13 at 08:48
  • 2
    The original question was to replace all system.out calls. Nothing in OP indicates that output strings with parantheses can be discounted. Nor can you discount `System.out.println(someobject.toString())` – Taemyr Sep 11 '13 at 09:46
  • @Taemyr @f1sh this `/System\.out\.println\((?<=\().*(?=\))\)/` does all the work, stop yelling & look [my edit](http://phpfiddle.org/lite/code/s7v-mt0) – vladkras Sep 11 '13 at 11:43
  • 1
    I have removed my downvote. However I feel I have to point out that `/System\.out\.println\((?<=\().*(?=\))\)/` is a difficult way of writing `/System\.out\.println\(.*\)/` – Taemyr Sep 12 '13 at 07:07