0

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

Satya Vejus
  • 313
  • 1
  • 3
  • 11
  • 5
    Why are you using `replaceAll` if you do not use regex? Use `replace` instead (it also replaces all occurrences, but without using regex).The naming might be a bit misleading. – Zabuzard Jul 13 '20 at 20:31
  • 4
    I think you are not assigning the result to `String lr` – The fourth bird Jul 13 '20 at 20:32
  • 1
    `replaceAll` does not modify the string `lr`, instead it returns the new result. So you have to update the variable `lr = lr.replaceAll(...)` with the returned result. – Zabuzard Jul 13 '20 at 20:32
  • What is your desired output? – dnault Jul 13 '20 at 20:33
  • 2
    Use `replace` instead of `replaceAll` as `replceAll` uses regex syntax where `(` and many other characters have special meaning and don't actually represent what we see like `(`. `replace` doesn't support regex syntax and (surprise surprise) replaces all occurrences of selected text. Also Strings are **immutable** which means we can't modify them. Because of that any methods like `replace` return *separate String* based on original one so you need to reassign them to original variable like `lr = lr.replace("foo", "bar");`. – Pshemo Jul 13 '20 at 20:40
  • using `replace` worked. Thanks! – Satya Vejus Jul 13 '20 at 20:41
  • You can use replacAll, just make these substitutions `"[ & ]"` -> `" & "` `"[ or ]"` -> `" or "` but it's hard to tell your meaning. And, either way `As you can see, it replaces all & and or's` it will remove the & and or's –  Jul 13 '20 at 20:46
  • @Maxt8r I am literally trying to remove the brackets as well, so replace was the best solution. But I understand my question was probably phrased a bit confusingly :) – Satya Vejus Jul 15 '20 at 04:11
  • But since you don't show an example of before and after data and to me, I can't glean from your regex what you're trying to do, it's not possible for _me_ to help you at this time. –  Jul 15 '20 at 17:15

0 Answers0