I'm trying to normalize any newlines or escaped newlines in a string to an escaped unix newline. I cannot figure out why this doesn't work:
Pattern EOL = Pattern.compile("(\\\\r)?\\\\n|\r?\n");
final String escapedEOL = "\\\\n";
System.out.println(EOL.matcher("asdf\njkl;").replaceAll(escapedEOL));
System.out.println(EOL.matcher("asdf\n").replaceAll(escapedEOL));
System.out.println(EOL.matcher("asdf\r\njkl;").replaceAll(escapedEOL));
System.out.println(EOL.matcher("asdf\r\n").replaceAll(escapedEOL));
System.out.println(EOL.matcher("asdf\\r\\njkl;").replaceAll(escapedEOL));
System.out.println(EOL.matcher("asdf\\r\\n").replaceAll(escapedEOL));
Result:
asdf\njkl;
asdf
asdf\njkl;
asdf\n
asdf\njkl;
asdf\n
Done
Can anyone shed any light on this? I realize I could split this into two calls but now I'm curious...
EDIT: Looks like I should have searched harder for similar problems. Looks like quantifiers with groups should be avoided in Java 7.
Pattern EOL = Pattern.compile("\\\\n|\\\\r\\\\n|\r?\n")
Works also.