[英]Regex findall() python returning empty list
I am trying to count the number of times a user entered regular expression shows up in any file, but the list created by findall keeps returning as empty.... help is appreciated!我正在尝试计算用户输入的正则表达式出现在任何文件中的次数,但由 findall 创建的列表一直返回为空......感谢帮助!
reg = input('Enter a regular expression ')
fname = input('Enter file name ')
try:
fhand = open(fname, 'r')
except:
print('Cannot open file')
quit()
for line in fhand:
line = line.strip()
matches = re.findall('(reg)', line)
print(matches, len(matches))
print(fname, 'had', len(matches), 'that matched', reg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.