Search code examples
pythonreddit

exact word find - reddit bot


So I've been working on a reddit bot and have run into one issue. Lets say for example I trying to find all comments with the word "man" in it, the bot will find comments with this word but will also find comments with man in a word. For example "woman" . I only want it to find the exact word man.

I am using the package "praw" . I know this is going to be a really easy fix but for some reason I cant figure it out. This is the code to find the word in a comment.

if "man" in comment.body:

if you need to see more of the code just let me know. Any help would be great. Also I am using Python to make this bot.


Solution

  • The word "man" appears exactly in the word "woman". You need to be more specific in your search, and that depends on the content of the comments. Perhaps you can search for " man" (but that would still count cases like " manage"), so perhaps you can do a regex search for whitespace followed by the string "man" followed by either whitespace or punctuation?