在字符串中查找类似的单词[复制]

This question already has an answer here:

I am trying to fetch similar words in a string. Suppose I have got a set of 5 keywords.

cats, dogs, animals, food, water

And I have a string like this

"Cat's an animal but different from a dog. Food's not same for both of them. But they both drink water."

If I try to use strpos in this case, I may find only one similar word "water" but in fact there are others too. What can help me achieve to detect all keywords in the sentence mentioned above?

</div>

Its possible you have an issue with case sensitivity. Try using the stripos() function. It could also be an issue with pluralization, or contractions, in which case a regular expression might make this easier.

For using regex -
This might mitigate contractions and boundary punctuation and such.
A more elaborate technique would be needed if detecting similarities
down to the character level.

(?i)(?<!\S)(?:cat(?:'?s)?|dog(?:'?s)?|animal(?:'?s)?|food|water)(?:(?=\p{P})|(?!\S))

Formatted:

 (?i)
 (?<! \S )
 (?:
      cat
      (?: '?s )?
   |  dog
      (?: '?s )?
   |  animal
      (?: '?s )?
   |  food
   |  water
 )
 (?:
      (?= \p{P} )
   |  (?! \S )
 )