使用正则表达式进行日志解析 [英] Log Parsing with Regex

查看:1446
本文介绍了使用正则表达式进行日志解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python使用regex解析Apache Log,并将其分配给单独的变量.

I'm trying to parse an Apache Log with regex using Python and assign it to separate variables.

ACCESS_LOG_PATTERN = '^(\S+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] "(\S+) (\S+)\s*(\S+)\s*" (\d{3}) (\S+)'

logLine='127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET /images/launch-logo.gif HTTP/1.0" 200 1839'

我将解析并将其分组为以下变量:

I will parse and group it into the following variable:

match = re.search(APACHE_ACCESS_LOG_PATTERN, logLine)



    host          = match.group(1)

    client_identd = match.group(2)

    user_id       = match.group(3)

    date_time     = match.group(4)

    method        = match.group(5)

    endpoint      = match.group(6)

    protocol      = match.group(7)

    response_code = int(match.group(8))

    content_size  = match.group(9)

正则表达式模式在日志行中工作正常,但在以下情况下解析/正则表达式匹配失败:

The regex pattern is working fine for the log line, but the parsing/regex match fails for the following case:

'127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET /" 200 1839'

'127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET / " 200 1839'

我该如何解决?

推荐答案

您需要通过添加?使group 7为可选.使用以下正则表达式:

You need to make your group 7 optional by adding a ?. Use the following regex:

^(\S+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] "(\S+) (\S+)\s*(\S+)?\s*" (\d{3}) (\S+)
                                                                 ↑

请参见演示

这篇关于使用正则表达式进行日志解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆