fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.regex.Pattern;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. Pattern EOL = Pattern.compile("((\\\\r)?\\\\n)|\r?\n");
  11. final String escapedEOL = "\\\\n";
  12.  
  13. System.out.println(EOL.matcher("asdf\njkl;").replaceAll(escapedEOL));
  14. System.out.println(EOL.matcher("asdf\n").replaceAll(escapedEOL));
  15. System.out.println(EOL.matcher("asdf\r\njkl;").replaceAll(escapedEOL));
  16. System.out.println(EOL.matcher("asdf\r\n").replaceAll(escapedEOL));
  17. System.out.println(EOL.matcher("asdf\\r\\njkl;").replaceAll(escapedEOL));
  18. System.out.println(EOL.matcher("asdf\\r\\n").replaceAll(escapedEOL));
  19.  
  20.  
  21. }
  22. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
asdf\njkl;
asdf\n
asdf\njkl;
asdf\n
asdf\njkl;
asdf\n