Search code examples
javaregexstringreplacereplaceall

Java String ReplaceAll (Regex Help)


I have the following String:

String str1= "ABCD";

I want the following String

String str2 = "AD"

Therefore, I would like something along the lines of this:

String str2 = str1.replaceAll("/* SOME REGEX HERE */", "");

How can I write the regex so that both "B" and "C" are replaced?


Solution

  • You could write it as...

    String str2 = str1.replaceAll("[BC]", "");