1

While debugging code, I added a bunch of lines in the form of

System.out.println("DEBUG: (Some statement)");
System.out.println("DEBUG: (Some statement)" + (Some expression));
System.out.println("DEBUG: (Some statement)" + (Some expression) + "Some other text");
System.out.println("DEBUG: (Some statement)" 
    + (Some expression)
    + "(Some other statement)");
System.out
    .println("DEBUG: (Some statement)" 
        + (Some expression)
        + "(Some other statement)");

where (Some expression) and (some statement) are expressions and statements respectively.

Now that I'm done debugging the code, I would like to be able to comment it out using find and replace, and to uncomment it later, if more bugs appear.

I have a regex that works for the single-line statements. (System.out.println("DEBUG:[^,]+"\n)

Is there a regex that works for the multiline statements?

0az
  • 350
  • 7
  • 18

1 Answers1

1

I suggest go for this solution. Check this post.

Any way regular expression as you asked:

Commenting

Find string: (?m)(?s)(System[\W.]*?out[\W.]*print[\w]*\(\"DEBUG:(.*?);)

Replace string: /*\1*/

Un commenting

Find string:

(?m)(?s)/\*((?m)(?s)(System[\W.]*?out[\W.]*print[\w]*\(\"DEBUG:(.*?);))\*/

Replace string: \1

Refer meta character

Community
  • 1
  • 1
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • Thanks! I'll try it when I get back to my laptop. – 0az Mar 12 '15 at 05:12
  • One issue with the regex is that I've been using single line comments, though that's a minor issue and easy to fix. Another issue is that the first regex also matches System.in, System.err.println and anything that starts with System., which is an issue because I have a good amount of those. I'd upvote, but I don't have the rep :(. – 0az Mar 12 '15 at 23:10
  • @alphadelta Now I changed the regular expression. Test it if it works then accept and upvote my answer – Chandrayya G K Mar 20 '15 at 12:59
  • Works perfectly. Sorry for the late response; I've been busy for the past couple days. – 0az Mar 25 '15 at 01:16