计数正则表达式在列表中找到

时间:2017-11-02 06:26:57

标签: python python-3.x python-3.5

我一直试图弄清楚如何计算我的正则表达式在列表中匹配的次数

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循环获得的结果,我正在阅读并发现我应该使用function 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"

2 个答案:

答案 0 :(得分:2)

\d{200}

匹配长度为200位的数字,而不是数字200。

试试这个正则表达式:

print(re.findall(r"\b200\b", element)))

这将匹配200(并且,由于word boundary anchors,请避免使用12002000等数字。)

答案 1 :(得分: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()