Home > Software design >  Using python, find specific string but only if not used in other words
Using python, find specific string but only if not used in other words

Time:12-12

I'm trying to make my own version of a popular reddit bot. These are not the values I will be using for my bot, but just to convey what I'm trying to do...

I have a JSON file converted into a list that looks like this:

['bobby b', 'bobbyb', 'bobby-b']

What I want to do is grab the text from a comment, and search it for any instances of the list items, BUT I don't want to find any instances where the list items begin or end in a different word.

So here are two example test strings:

#1: "Hi, I'm bobby b. I want to yell funny quotes at you."

#2: "Hi, I'm bobby baratheon. I don't want to yell quotes at you."

I want to write a search that will return true for #1 but not for #2. Please help.

I have searched many similar questions, but I'm a newbie to python and regex, so I don't know how to apply them to my case.

Here's the starting code that returns true for both examples:

x = ['bobby b', 'bobbyb', 'bobby-b']
text = comment.body

for keyword in x:

  if re.search(keyword, text, re.IGNORECASE):
    print(keyword)

CodePudding user response:

If I understand the question correctly, you want to get True only if a word in a comment exactly matches a string in the list.

I'm not sure if this is the best solution but still, here's how I solved it,


text  = " " # added this line

for keyword in x:

  keyword  = " "  # added this line

  if re.search(keyword, text, re.IGNORECASE):
      print(keyword)

CodePudding user response:

Update: I've gotten some answers from other places and have been able to find something that appears to work. Here's my new code:

import re

# a Python object (dict):
x = ['bobby', 'bobbyb', 'bobby-b']

text = 'hi im bobby baratheon. how you doing?'


for keyword in x:

  keyword = rf'\b{keyword}\b'

  # Do a case-insensitive search
  if re.search(keyword, text, re.IGNORECASE):
    return True
  else:
    return False
  • Related