我是python的新手,我已经开始学习一些正则表达式。我一直在尝试匹配字符串中的某些文本,但遇到了一些我不理解的东西。这是我的代码:
import re
pattern1 = r'\b\w+\b,\s\b\w+\b'
pattern2 = r'\b\w+\b,\s\b\w+\b,'
# pattern1 produces expected result
with open('test_sentence.txt', 'r') as input_f:
for line in input_f:
word = re.search(pattern1, line)
print word.group()
# pattern 2, same as pattern1 but with additional ',' at the end
# does not work.
with open('test_sentence.txt', 'r') as input_f:
for line in input_f:
word = re.search(pattern2, line)
print word.group()
下面是 test_sentence.txt 的内容:
I need to buy are bacon, cheese and eggs.
I also need to buy milk, cheese, and bacon.
What's your favorite: milk, cheese or eggs.
What's my favorite: milk, bacon, or eggs.
我不明白为什么pattern2
不起作用它none-type object has no attribute group
在引用时会引发错误print word.group()
。我相信这意味着它无法找到“pattern2”的正则表达式代码的匹配项。为什么最后的额外内容,
会导致此问题?为什么它不简单地搭配milk, cheese,' and
牛奶,培根,`?