fork download
  1. class Ideone {
  2. public static void main (String[] args) {
  3. String[] strs = {
  4. "4-2-2-1",
  5. "4 -2 - 2 - 1",
  6. "1-2-3-4-5",
  7. "1-5",
  8. "4 - 5"};
  9.  
  10. for (String s1 : strs) {
  11. String s2 = s1.matches("(\\d+\\D+){2,}\\d") ? s1.replaceAll("\\D+", " ") : s1;
  12. System.out.println(s2);
  13. }
  14. }
  15. }
Success #stdin #stdout 0.07s 33532KB
stdin
Standard input is empty
stdout
4 2 2 1
4 2 2 1
1 2 3 4 5
1-5
4 - 5