3
举例来说,我想检查单词test
是否在字符串中。通常,我只是:正确检查单词是否在字符串中?
if 'test' in theString
但我想确保它是实际的单词,而不仅仅是字符串。例如,test
中的“这是可恶的”会产生误报。我可以检查以确保它包含(\s)test(\s)
(空格之前和之后),但是“......准备测试!”会产生假阴性。看来我唯一的选择是这样的:
if ' test ' in theString or ' test.' in theString or ' test!' in theString or.....
有没有办法正确地做到这一点,像?
8分钟即可到达。 *叹*。感谢您的答案,没有想到正则表达式,尽管在示例中使用它! – tkbx 2012-08-03 23:58:39