I have a string that looks like this:
String lr = "(JG- or (Ss*s+ & ) or SIs- or (Js- & ( or )) or Os- or [ & Xd- & (Xc+ or [[[[()]]]]) & MX-] or [[[[()]]]])) or ( & dSJls+) or ( & dSJrs-) or YS+ )) or ( & [AN+]) or G+))) or (MXs+ & ((Ss*s+ & ) or SIs- or (Js- & ( or )) or Os- or [ & Xd- & (Xc+ or [[[[()]]]]) & MX-] or [[[[()]]]] or ( & dSJls+) or ( & dSJrs-))) or ( & & Wa- & ) or [ & (( & Wc- & & (Xc+ or [()]) & (Qd+ or Wq+)) or ( & & (Xc+ or [[()]]) & [dCOa+])) or ( & Xc+ & S**i+)]) ";
And here is my code to remove expressions such as " & )"
and "( or )"
:
lr=lr.replaceAll("[ & ]", ""); lr=lr.replaceAll("( & )", "");
lr=lr.replaceAll("[ or ]", ""); lr=lr.replaceAll("( or )", "");
lr=lr.replaceAll("\\( or ", "("); lr=lr.replaceAll("\\( & ", "(");
lr=lr.replaceAll(" or \\)", ")"); lr=lr.replaceAll(" & \\)", ")");
However, the result is
(JG-(Ss*s+)SIs-(Js-())Os-[Xd-(Xc+[[[[()]]]])MX-][[[[()]]]]))(dSJls+)(dSJs-)YS+))([AN+])G+)))(MXs+((Ss*s+)SIs-(Js-())Os-[Xd-(Xc+[[[[()]]]])MX-][[[[()]]]](dSJls+)(dSJs-)))(Wa-)[((Wc-(Xc+[()])(Qd+Wq+))((Xc+[[()]])[dCOa+]))(Xc+S**i+)])
afterwards.
As you can see, it replaces all & and or's which is clearly not intended from the replaceAlls. What am I doing wrong? At first I thought it might be something to do with the \ before the parentheses, so I removed them, but then I got errors: Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 3 or )
Thanks, Satya