I have a string coming from UI that may contains control characters, and I want to remove all control characters except carriage returns, line feeds
This seems to be an option
String s = "\u0001\t\r\n".replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", ""); for (char c : s.toCharArray()) { System.out.print((int) c + " "); }
prints 9 13 10 just like you said "except carriage returns, line feeds, and tabs".
9 13 10