I am trying to replace all occurrences of the special character with new line except when there is a ? character preceeding the character.Input string is HJK'ABCP?'QR2'SER' and want the output as
HJK'
ABCP?'QR2'
SER'
Code:
String character="'";
String str="HJK'ABCP?'QR2'SER'";
str=str.replaceAll("(?<!\\?)"+character, character+"\r\n");
System.out.println(str);
I get o/p as expected:-
HJK'
ABCP?'QR2'
SER'
Problem is the special character could vary and has to be replaced with new line (expcept when preceeded by ?)
str.replaceAll("(?
doesn't work for all cases.eg. doesn't work as expected when character is * or [
Can anyone tell what's wrong?