2

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 not give error and why it replaced some words.

2
  • 3
    I recommend finding a repeatable testcase. Commented May 28, 2011 at 0:04
  • Is it a case issue? Could you provide a short string which fails? Commented May 28, 2011 at 0:06

2 Answers 2

3

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:

  1. You need to assign the return value (string are immutable in Java, as also mentioned by Ehran)
  2. There are some non-printable characters in the string;
  3. There is an encoding issue when you read the input and compare;
  4. You word is delimited by something that doesn't register a boundary.
  5. 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)
  6. 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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. I think first two could be the reason. Because it only happens when I read a file and then search replace. How to avoid/control them?
If you are on a unix/linux machine, try a cat -e MY_FILE to display non-printable characters, to check the source files and the input files.
@user751637: For instance, are there non-breaking spaces in this "text" file?
@user751637: please provide your input data here as text or as a file uploaded somewhere else, and do provide more of your code so we can see how you read the input as well. We asked for more details a few times now. I updated my answers with some more possibilities and suggestions, but without the data digging deeper will be difficult.
Haylen, thanks for your quick and informative answers. I updated my question. I found the solution but I do not know why and how !. Could you please put some lights on that issue?
|
2

"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", ""));
    } 

}

2 Comments

damned, you beat me to it I think, I'll credit you for that in my answer.
Thanks, I am already assigning it to a String. I updated my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.