fork(5) download
  1. /* http://es.stackoverflow.com/q/42298/127 */
  2.  
  3.  
  4. import java.util.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. final String regex = "\\b(\\w+)(?> +\\1)+\\b";
  15. final Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  16.  
  17. final String reemplazo = "$1";
  18.  
  19. Scanner in = new Scanner(System.in);
  20. String input = in.nextLine();
  21. Matcher m = p.matcher(input);
  22.  
  23. //Se reemplazan todas las ocurrencias
  24. input = m.replaceAll(reemplazo);
  25.  
  26. System.out.println(input);
  27. }
  28. }
Success #stdin #stdout 0.06s 711680KB
stdin
Hola hola hOla
stdout
Hola