I have the following code which replaces control characters:
String str = "Apple\n"
+ "\n"
+ "\n"
+ "\n"
+ "> --"
+ "\n"
+ "\n"
+ "Mango\n"
+ "\n"
+ "\n"
+ "Kiwi\n"
+ "Papaya\n"
+ "\n"
+ "Orange\n";
str = str.replaceAll("(?m)((^[\\p{C}]*)|([\\p{C}]*$))", "");
I want to remove all control characters except the new line (\n) from the start of the line and the end of the line. The problem I am having in that \n
is a part of \\p{C}
set. How do I modify this regex to ensure it replaces all characters except the \n
character.