fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8.  
  9. class Ideone {
  10. public static void main (String[] args) throws java.lang.Exception {
  11. String s = "w ww www wwww wwwww wwwwwww wwwwww";
  12. Pattern p = Pattern.compile("\\b\\w{3,5}\\b");
  13. Matcher m = p.matcher(s);
  14. int matches = 0;
  15. while (m.find()){
  16. matches++;
  17. }
  18. if (matches % 2 == 1) {
  19. matches--;
  20. }
  21. m.reset();
  22. int counter = 0;
  23. while (counter++ < matches && m.find()){
  24. m.appendReplacement(ss, "");
  25. }
  26. m.appendTail(ss);
  27. System.out.println(ss.toString());
  28. }
  29. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
w ww   wwwww wwwwwww wwwwww