fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s = "0 - 22 1985--324"; // 022-198-53-24
  13. run(s);
  14. s = "555372654"; // 555-372-654
  15. run(s);
  16. s = "12-10 23 5555 8361";
  17. run(s);
  18. }
  19. public static void run(String abc) {
  20. String hello = abc.replaceAll("[^a-zA-Z0-9]", "");
  21. hello = hello.replaceAll("(?<=\\G\\d{3})(?!$)", "-");
  22. System.out.println(hello.replaceAll("\\b(\\d{2})(\\d+)-(\\d)$", "$1-$2$3"));
  23. }
  24. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
022-198-53-24
555-372-654
121-023-555-583-61