【发布时间】:2021-04-24 04:37:11
【问题描述】:
下面是从文件中查找所有大写单词并将它们添加到列表中的代码,我该如何更改它以便只将以大写开头的单词添加到列表中。
import re
matches = []
regex = r"\b[A-Z]\w*"
filename = r'C:\Users\Documents\romeo.txt'
with open(filename, 'r') as f:
for line in f:
matches += re.findall(regex, line)
print(matches)
文件:
Hello, How are YOU
输出:
[Hello,How]
你不应该被包含在输出中。
【问题讨论】:
标签: python-3.x regex file python-re findall