String exact match

后端 未结 7 1685
悲哀的现实
悲哀的现实 2020-11-29 08:13

I have a string in which the word \"LOCAL\" occurs many times. I used the find() function to search for this word but it returns another word \"Locally\" as wel

7条回答
  •  孤独总比滥情好
    2020-11-29 08:54

    Below you can use simple function.

    def find_word(text, search):
    
       result = re.findall('\\b'+search+'\\b', text, flags=re.IGNORECASE)
       if len(result)>0:
          return True
       else:
          return False
    

    Using:

    text = "Hello, LOCAL locally local test local."
    search = "local"
    if find_word(text, search):
      print "i Got it..."
    else:
      print ":("
    

提交回复
热议问题