Java replace all
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?
Edit
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 no开发者_开发知识库t give error and why it replaced some words.
Answer after updated question
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.
Original answer
Kept for similar errors encountered by others.
This obviously works, so there are only a few options left:
- You need to assign the return value (string are immutable in Java, as also mentioned by Ehran)
- There are some non-printable characters in the string;
- There is an encoding issue when you read the input and compare;
- You word is delimited by something that doesn't register a boundary.
- You want a case-insensitive search (use
Pattern.compile(regex, flags).matcher(str).replaceAll(repl)
instead, with a CASE_INSENTIVE flag to compile the pattern) - There's something wrong somewhere else that we cannot see because you give:
- neither the whole code
- nor the input data.
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.
"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", ""));
}
}
精彩评论