python regex to detect a word exists

前端 未结 3 1853
庸人自扰
庸人自扰 2020-12-18 11:04

I want to detect whether a word is in a sentence using python regex. Also, want to be able to negate it.

import re
re.match(r\'(?=.*\\bfoo\\b)\', \'bar red f         


        
3条回答
  •  自闭症患者
    2020-12-18 11:56

    You need the .* because re.match() tries to match the pattern to the beginning of the string. If you want to search the whole string, use re.search().

    Just as you can do if re.search(...):, you can also do if not re.search(...):

提交回复
热议问题