计数在列表中找到的正则表达式
Count regex found in a list
我一直在想办法计算我的正则表达式在列表中匹配的次数
def total_200():
load = loadFiles()
for element in load:
print(re.findall("\d{200}\s", element))
if __name__ == "__main__":
total_200()
这将打印
0
0
0
0
对于每个找到的值,我不确定为什么它打印 0,但我需要找出的是如何计算我从 for 循环中得到的结果,我正在阅读并发现我应该使用函数 len()
我试过使用它。像这样,
print(len(re.findall("\d{200}\s", element)))
Python RegEx, match words in string and get count
更新
这是我用来加载日志文件的函数
def loadFiles():
access_0 = open('apachelog.txt','r')
line_0 = access_0.read().splitlines() #readlines() read the text line per line
access_0.close()
return line_0
日志文件的格式:
10.10.10.10 - - [29/Aug/2017:04:56:06 -0400] "GET /isomaster/download/ HTTP/1.1" 301 - "-" "curl/7.51.0"
也许试试这个:-
def loadFiles():
access_0 = open('apachelog.txt','r')
line_0 = access_0.read() #.splitlines() #readlines() read the text line per line
access_0.close()
return line_0
def total_200():
load = loadFiles()
#for element in load:
#print(re.findall("\d{200}\s", element))
print(load.count("200 "))
if __name__ == "__main__":
total_200()
\d{200}
匹配长度为 200 位的数字,而不是数字 200。
试试这个正则表达式:
print(re.findall(r"\b200\b", element)))
这将匹配 200
(并且,感谢 word boundary anchors,避免像 1200
、2000
等数字)。
我一直在想办法计算我的正则表达式在列表中匹配的次数
def total_200():
load = loadFiles()
for element in load:
print(re.findall("\d{200}\s", element))
if __name__ == "__main__":
total_200()
这将打印
0
0
0
0
对于每个找到的值,我不确定为什么它打印 0,但我需要找出的是如何计算我从 for 循环中得到的结果,我正在阅读并发现我应该使用函数 len()
我试过使用它。像这样,
print(len(re.findall("\d{200}\s", element)))
Python RegEx, match words in string and get count
更新
这是我用来加载日志文件的函数
def loadFiles():
access_0 = open('apachelog.txt','r')
line_0 = access_0.read().splitlines() #readlines() read the text line per line
access_0.close()
return line_0
日志文件的格式:
10.10.10.10 - - [29/Aug/2017:04:56:06 -0400] "GET /isomaster/download/ HTTP/1.1" 301 - "-" "curl/7.51.0"
也许试试这个:-
def loadFiles():
access_0 = open('apachelog.txt','r')
line_0 = access_0.read() #.splitlines() #readlines() read the text line per line
access_0.close()
return line_0
def total_200():
load = loadFiles()
#for element in load:
#print(re.findall("\d{200}\s", element))
print(load.count("200 "))
if __name__ == "__main__":
total_200()
\d{200}
匹配长度为 200 位的数字,而不是数字 200。
试试这个正则表达式:
print(re.findall(r"\b200\b", element)))
这将匹配 200
(并且,感谢 word boundary anchors,避免像 1200
、2000
等数字)。