Reputation: 317
I am trying to replace some words from a string and use following code
someMoreString = someString.replaceAll("\\b(shoe||apple|search|employee|expert)\\b", "");
It works fine. Today I found that it does not replace some words. The replace list is long
I cannot verify all. However, I found search
word was never replaced in files. I doubt that there could be more cases like this.
Any idea why is it happening? How can I stop this?
Thank you all for your answers. I found the solution :-)
I was adding two bar signs in replace string, that cause for this problem. For example:
someMoreString = someString.replaceAll("\b(shoe||apple|search|employee|expert)\b", "");
I do not know, why it did not give error and why it replaced some words.
Upvotes: 2
Views: 9042
Reputation: 22663
The issue with your double pipe was that it will look for matches to replace, and hence replace any single occurrence of something matching the first word ("shoe"), and if it doesn't work look for the next potential match, which is an empty string (between the 2 pipes). So you'd find these matches and replace them (ironically) with empty strings as well. As a match was found for this position, it switches to the next potential positions and doesn't check the other words for that one.
Quite likely, any word after the doubled-pipe was never replaced.
It didn't yield an error because the syntax is valid and there are legitimate cases where you'd want to look for empty strings to insert characters.
Kept for similar errors encountered by others.
This obviously works, so there are only a few options left:
Pattern.compile(regex, flags).matcher(str).replaceAll(repl)
instead, with a CASE_INSENTIVE flag to compile the pattern)Please provide more code and your input excerpt.
If you read from a socket, do make sure as well that you specify the right headers for your request and that you use valid content type and character encoding. Please make also sure that you are not using a strange encoding on your source files and your input data files.
This is partially re-written off of another answer I gave on this question about why the java String.contains method does not return found matches correctly.
Upvotes: 3
Reputation: 5327
"Today I found that it does not replace some words"
I think without assignment you don't replace the words really, neither search nor other words. String operations are immutable, try this:
someString = someString.replaceAll("\\b(apple|search|employee|expert)\\b", "");
a sample test:
public class StringTests {
@Test
public void replaceAllTest() {
String someString ="bla bla search bla";
System.out.println(someString);
someString.replaceAll("\\b(apple|search|employee|expert)\\b", "");
System.out.println(someString);
someString = someString.replaceAll("\\b(apple|search|employee|expert)\\b", "");
System.out.println(someString);
assertEquals(someString, someString.replaceAll("\\b(apple|search|employee|expert)\\b", ""));
}
}
Upvotes: 2