【发布时间】:2015-09-17 12:31:00
【问题描述】:
我正在尝试将字符串“hello world”与句子匹配。我认为这意味着它会在句子中搜索该字符串并返回一个表示成功的值。
但是当我尝试这段代码时,打印出来的都是“无”。
import re
sentence = "why do we write hello world so often?"
match1 = re.match('hello world', sentence)
print match1
【问题讨论】:
-
这不是你应该使用正则表达式的地方。使用
in。 -
if 'hello world' in sentence:。如果你需要更复杂的东西,你应该考虑模糊搜索。