import re
message = "I'm new and this is new my account."
message_with_hi = "what's up, I'm saying hi"
pattern = r'\bhi\b' # \b is word boundary
r = re.findall(pattern, message)
r2 = re.findall(pattern, message_with_hi)
print(r) # prints []
print(r2) # prints ['hi']
您可以使用正则表达式
这也包括
message = "I am saying hi!"
等情况一个优雅的解决方案是:
或者使用正则表达式:https://stackoverflow.com/a/5320179/4585157
你也可以试试这个,但我想第一个答案更好
相关问题 更多 >
编程相关推荐