fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String str = "AB CD #E F# #GH I# JK L M";
  9. String repl = str.replaceAll("\\s+(?=(?:(?:[^#]*#){2})*[^#]*$)", "");
  10. System.out.printf("Replaced: [%s]%n", repl);
  11. }
  12. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Replaced: [ABCD#E     F##GH   I#JKLM]