Nick Heiner
Nick Heiner

Reputation: 122450

Java: str.replaceAll() not working

I'm trying to cleanse one String from another.

before = before.replaceAll(Constants.GENE_START_SEQUENCE, "");

And yet, the following assertion sometimes fails:

assert before.indexOf(Constants.GENE_START_SEQUENCE) == -1 : before;

This is what the assert spits out:

IIAOOOCOAAAOCCIOOOACAIAOACICOOIAIOOICIIOIIOICOICCCOOAOICOCOOIIOOAOAACIIOCCICIOIII

Upvotes: 1

Views: 3782

Answers (2)

sepp2k
sepp2k

Reputation: 370172

replaceAll only replaces occurences of the pattern in the original string. If the pattern reoccurs as a result of the replacement, this new occurence won't be replaced. Example:

"XXYY".replaceAll("XY", "");

This will find one occurence of "XY" (at index 1) and then replace that with "". The result will be "XY". If you want to prevent this from happening, you have to rexecute replaceAll, until replaceAll stops finding a match.

String string = "XXYY";
String oldString;
do {
  oldString = string;
  string = string.replaceAll("XY", "");
} while(!string.equals(oldString));
// string will now be ""

Upvotes: 11

p3t0r
p3t0r

Reputation: 1980

You should make sure that Constants.GENE_START_SEQUENCE is a valid regex pattern. If it isn't supposed to be a regular expression you should escape it using the quote method on java.util.regex.Pattern.

Upvotes: 2

Related Questions