void () { String in = """ try { String[] a = {"a"}; System.out.println("blah"); } finally { } """; String what = """ try { '_statement*; } finally { \s }"""; String replacement = "$statement$;"; String expected = """ String[] a = {"a"}; System.out.println("blah"); """; assertEquals(expected, replace(in, what, replacement)); String in2 = """ try { if (args == null) return ; while(true) return ; System.out.println("blah2"); } finally { }"""; String expected_2 = """ if (args == null) return ; while(true) return ; System.out.println("blah2");"""; assertEquals(expected_2, replace(in2, what, replacement)); String in3 = """ { try { System.out.println("blah1"); System.out.println("blah2"); } finally { } }"""; String expected_3 = """ { System.out.println("blah1"); System.out.println("blah2"); }"""; assertEquals(expected_3, replace(in3, what, replacement)); String in4 = """ { try { System.out.println("blah1"); // indented comment System.out.println("blah2"); } finally { } }"""; String expected_4 = """ { System.out.println("blah1"); // indented comment System.out.println("blah2"); }"""; assertEquals(expected_4, replace(in4, what, replacement)); String in5 = """ class X { public void visitDocTag(String tag) { String psiDocTagValue = null; boolean isTypedValue = false; {} } }"""; String what5 = """ void '_m('_T '_p) { '_st*; }"""; String replacement5 = """ void $m$($T$ $p$) { System.out.println(); $st$; }\ """; String expected5 = """ class X { public void visitDocTag(String tag) { System.out.println(); String psiDocTagValue = null; boolean isTypedValue = false; {} } }"""; assertEquals(expected5, replace(in5, what5, replacement5)); }