Search code examples
javaregexword-boundary

Replace all words in java


I'm using the java replaceAll() method for replace matching words in a string. In my case If this word is next to a comma (,) fullstop (.) or anything else like that, this word is not replacing.

example : and. and, and; and(

This is the code:

body = body.replaceAll("(?i) "+knownWord + " ", replaceWord);

Can anyone please suggest me an regular expression which is capable of identifying all the words in this string?


Solution

  • If you want to match specific knownWord do:

      body = body.replaceAll("(?i)\\b"+knownWord + "\\b", replaceWord);
    

    I think what you were looking for is the \\b (word boundary) it is used to detect where words start/end, so commas or dots should no longer be a problem then.

    More detailed example in response to your comment:

     String body = "I'm going to school. ";
     String knownWord = "school";
     String replaceWord = "shop";
     System.out.println(body.replaceAll("(?i)\\b"+knownWord + "\\b", replaceWord));
    

    The above will print out the following:

    I'm going to shop.