比如说,我想要检查一个字符串中是否包含单词 test
。通常来说,我会这样做:
if 'test' in theString
但我希望确认的是实际单词,而不仅仅是字符串。例如,在“这很可恶”中的test
将产生误报。我可以检查它是否包含(\s)test(\s)
(前后有空格),但是那么“...为测试做准备!”将产生误报。看来我唯一的选择是:
if ' test ' in theString or ' test.' in theString or ' test!' in theString or.....
有没有一种正确的方法可以这样做,类似于if 'test'.asword in theString
?