fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String s = "Jean-Pierre bought \"blue-green-red\" product-2345 and other blue-red stuff. yellow-black-white. product_a-b";
  11. StringBuffer result = new StringBuffer();
  12. Matcher m = Pattern.compile("(\"[^\"]*\")|\\b([a-zA-Z]+)-(?=[a-zA-Z]+\\b)").matcher(s);
  13. while (m.find()) {
  14. if (m.group(1) != null) {
  15. m.appendReplacement(result, m.group(0));
  16. } else {
  17. m.appendReplacement(result, m.group(2) + " ");
  18. }
  19. }
  20. m.appendTail(result);
  21. System.out.println(result.toString());
  22.  
  23. }
  24. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
Jean Pierre bought "blue-green-red" product-2345 and other blue red stuff. yellow black white. product_a-b