6 Replies Latest reply on Apr 22, 2010 4:25 PM by 807580

    Regular Expression - replaceAll() - how to replace words?

    807580
      Hiya,

      I have this regex to replace all instances of myWord:
      String oldWord = "oldWord";
      String newWord = "newWord";
      String sentence = "some sentence that contains " + oldWord;
      String newSentence = replaceWordsInSentence(sentence, oldWord, newWord);
      
      private String replaceWordsInSentence(String sentence, String oldWord, String newWord) {
          return sentence.replaceAll("\b" + oldWord + "\b", newWord);
      }
      ...it works in most instances, but when oldWord is at the end of the sentence it is not replaced. Presumably the problem is that "/b" is not a sufficient word boundary. Can someone help me out with the correct regular expression code?

      Thanks,
      James
        • 1. Re: Regular Expression - replaceAll() - how to replace words?
          JoachimSauer
          I see three problems:
          - You must use "\\b" instead of "\b" (remember: you need to escape the backslash for the string literal)
          - You should use Pattern.quote(oldWord) to avoid special characters to be interpreted as regex in oldWord
          - You should use Matcher.quoteReplacement(newWord) for a similar reason

          Fixing those made the code work for me.
          • 2. Re: Regular Expression - replaceAll() - how to replace words?
            807580
            Thanks for the reply. Can you show me the code - am a bit lost with points 2 and 3.
            • 3. Re: Regular Expression - replaceAll() - how to replace words?
              699554
              jhodgski wrote:
              Thanks for the reply. Can you show me the code - am a bit lost with points 2 and 3.
              Points 2 and 3 are an alternative. String.replaceAll() takes as its first argument, a regular expression. Regular expression syntax differs as many conflicts arise due to the power of the language, therefore to express the word boundary character, you need to escape the back slash itself. E.g. add an extract backslash to both "\b" and you are done.

              Mel
              • 4. Re: Regular Expression - replaceAll() - how to replace words?
                807580
                Mel, thanks for the reply. Maybe you misunderstood Joachim's post: he seemed to be saying there were 3 problems - not 3 solutions... and that he applied all 3 steps to create the single solution.
                • 5. Re: Regular Expression - replaceAll() - how to replace words?
                  699554
                  jhodgski wrote:
                  Mel, thanks for the reply. Maybe you misunderstood Joachim's post: he seemed to be saying there were 3 problems - not 3 solutions... and that he applied all 3 steps to create the single solution.
                  No I haven't misunderstood. There is a distinct difference between the phrases "You must" and "You should", but in any case the example is so elementary that put simply, all you need to do is heed the advice given.

                  Whats easier? Add the additional backslash as suggested and try it yourself, or wait hours for verification from the source of the solution?

                  Mel
                  • 6. Re: Regular Expression - replaceAll() - how to replace words?
                    807580
                    Mel, you did appear to misunderstand as you thought points 2 and 3 were alternatives, but you now recognise that they are additional "shoulds".

                    Of course, I applied the extra backslash as soon as Joachim advised. Maybe you don't agree with my rationale, but I prefer the complete solution that will work in all instances... so was simply waiting for him to post a code example that included the latter 2 points as (although I understood the point of them perfectly) I was not sure how to implement them.

                    Have come up with the following, expanded, method...
                        private String replaceWordsInSentence(String sentence, String oldWord, String newWord) {
                            return sentence.replaceAll("\\b" + Pattern.quote(oldWord) + "\\b", Matcher.quoteReplacement(newWord));
                        }
                    ...works fine with the tests I have run. Joachim, can you confirm this is correct.