I've made this simple script for practicing RegEx that is supposed to read text that I copied to the clipboard and count how many times the word "corona" is mentioned. But i keep getting
"IndexError: string index out of range" which I get at
matches.append(groups[0])
which I don't understand seeing as I'm starting the index at 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")
0.matches.append(groups[0]), right? That's because you might get a result from findall that is an empty string which, of course, can't be used with[0].