Python中有一种方法可以搜索输入字符串中的短语,然后返回如果有,则为1;否则为0?在Python中搜索短语的字符串输入
我希望它是这样工作的:
def findphrase(var):
if re.compile(r'\b({0})\b'.format(var), flags=re.IGNORECASE).search is True:
return 1
else:
return 0
def howareyou():
print("So",name, "how are you today?")
howis = input("")
if findphrase('not well')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('well')(howis) is 1:
print("That's good.")
elif findphrase('not bad')(howis) is 1:
print("Better than bad, I suppose.")
elif findphrase('bad')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('not good')(howis) is 1:
print("That's a shame. I hope you feel better soon.")
elif findphrase('good')(howis) is 1:
print("That's good.")
else:
print("I dont know how to respond to that. Keep in mind I am a work in progress. At some point I may know how to respond.")
完全可能的。看看're'模块文档。 – cge
还是'in'运营商,它告诉你一个字符串是否包含在另一个。 –
@ Rozza_15如果您的查询已解决,请不要忘记[接受下面的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) –