在python中搜索精确单词

2024-09-28 20:51:13 发布

您现在位置:Python中文网/ 问答频道 /正文

找到了休博思韦尔的答案

Python - Check If Word Is In A String

很有用,但现在我想让它发生

 findWholeWord('object')('object test')    # -> <match object>
 findWholeWord('object')('object-group')                   # -> None

有人吗?你知道吗


Tags: 答案intestnonestringifobjectis
2条回答
(?:^|(?<=\s))object(?=\s|$)

您可以将这个正则表达式与re.findall一起使用来检查它是否存在。你知道吗

像这样的

if re.findall(r"(?:^|(?<=\s))object(?=\s|$)",test_str):
    print "yes present"

使用正则表达式:

'object(?=[\W\D\S\s]*?)'

这应该管用。你知道吗

相关问题 更多 >