fork download
  1. import java.util.regex.*;
  2.  
  3. class Ideone {
  4. public static void main (String[] args) throws java.lang.Exception {
  5. // pattern that captures quoted strings ignoring all escaped quotes
  6. Pattern p = Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"");
  7.  
  8. String data1 = "\"Test Line wo line break\", \"Test Line \nwith line break\"\n\"Test Line2 wo line break\", \"Test Line2 \nwith line break\"\n";
  9.  
  10. // functional code to get all quotes strings and then remove all line
  11. // breaks from matched substrings
  12. String repl = p.matcher(data1).replaceAll(m -> m.group().replaceAll("\\R+", ""));
  13.  
  14. System.out.println(repl);
  15. }
  16. }
Success #stdin #stdout 0.1s 48388KB
stdin
Standard input is empty
stdout
"Test Line wo line break", "Test Line with line break"
"Test Line2 wo line break", "Test Line2 with line break"