我制作了这个简单的脚本来练习正则表达式,它应该读取我复制到剪贴板上的文本,并计算“corona”一词被提及的次数。但我一直在
"IndexError: string index out of range" which I get at
matches.append(groups[0])
我不明白,因为我从0开始索引
import re
import pyperclip
coronaRegEx = re.compile(r'Corona(virus)*', re.IGNORECASE)
text = str(pyperclip.paste())
matches = []
count = sum(matches)
for groups in coronaRegEx.findall(text):
matches.append(groups[0])
if len(matches) > 0:
pyperclip.copy(join(matches))
print("Found " + count + " of instances")
else:
print("No instances found")
我不知道你对这个问题的看法。我已简化为以下内容
看看这是否解决了你的问题。(这也处理空字符串角的情况)
相关问题 更多 >
编程相关推荐