Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:52:21+00:00 2026-05-22T20:52:21+00:00

I am trying to replace some words from a string and use following code

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:52:22+00:00Added an answer on May 22, 2026 at 8:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to modify the output stream to search/replace some XHTML tags returned
I'm trying to use this code to replace spaces with _, it works for
I'm trying to replace all the carriage return characters in a string obtained from
I am trying to use a Rake task to migrate some legacy data from
I'm trying to remove some excessive indention from a string, in this case it's
I'm trying to extract all words from a string into an array, but i
I'm trying to replace the programs that run from my startup directory with a
Using the .NET framework, I'm trying to replace double slash characters in a string
I'm trying to use System.IO.File.Replace to update a file, and it's throwing System.IOException if
I'm trying to replace all instances of a word, say foo between some HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.