I have a condition where I have to replace some character(special, non-print-able and other special character) from string as mention below
private static final String NON_ASCII_CHARACTERS = "[^\\x00-\\x7F]";
private static final String ASCII_CONTROL_CHARACTERS = "[\\p{Cntrl}&&[^\r\n\t]]";
private static final String NON_PRINTABLE_CHARACTERS = "\\p{C}";
stringValue.replaceAll(NON_ASCII_CHARACTERS, "").replaceAll(ASCII_CONTROL_CHARACTERS, "")
.replaceAll(NON_PRINTABLE_CHARACTERS, "");
can we refactor above code means we can use single "replaceAll" method and put all conditions inside?
is there any way please advice.
|to do an "or"."re1|re2"? Is this really such a mystery?ASCII_CONTROL_CHARACTERSimplies that you want to keep tabs and line breaks, butNON_PRINTABLE_CHARACTERSincludes them, so you end up removing them. In fact, tabs and line breaks are the only non printable characters left on the third replace operation. I think, you are better off thinking first, what you actually want to keep, which is only a small deviation from your first pattern, i.e.stringValue.replaceAll("[^\r\n\t\\x20-\\x7F]", "")and that’s it.