Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

This does as you specify:

(?<![.,])\b(\w+)\b(?![.,])

Regular expression visualization

Debuggex Demo

It finds (and captures) words as long as they are not next to commas or periods. Just add whatever punctuation marks you like to the character-classes, such as [.,?(].

Here is the regex escaped for a Java string: "(?<![.,])\\b(\\w+)\\b(?![.,])"

As far as ignoring case, just pass the CASE_INSENSITIVE flag to your Pattern object, such as with

Pattern p = Pattern.compile(theAbovePattern, Pattern.CASE_INSENSITIVE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top