Log Parsing with Regex

前端 未结 2 1177
你的背包
你的背包 2020-12-21 09:46

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[         


        
2条回答
  •  滥情空心
    2020-12-21 10:27

    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+)
                                                                     ↑
    

    See the DEMO

提交回复
热议问题