Log Parsing with Regex

前端 未结 2 1172
你的背包
你的背包 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

    0 讨论(0)
  • 2020-12-21 10:44
    import re
    
    
    HOST = r'^(?P<host>.*?)'
    SPACE = r'\s'
    IDENTITY = r'\S+'
    USER = r'\S+'
    TIME = r'(?P<time>\[.*?\])'
    REQUEST = r'\"(?P<request>.*?)\"'
    STATUS = r'(?P<status>\d{3})'
    SIZE = r'(?P<size>\S+)'
    
    REGEX = HOST+SPACE+IDENTITY+SPACE+USER+SPACE+TIME+SPACE+REQUEST+SPACE+STATUS+SPACE+SIZE+SPACE
    
    def parser(log_line):
        match = re.search(REGEX,log_line)
        return ( (match.group('host'),
                match.group('time'), 
                          match.group('request') , 
                          match.group('status') ,
                          match.group('size')
                         )
                       )
    
    
    logLine = """180.76.15.30 - - [24/Mar/2017:19:37:57 +0000] "GET /shop/page/32/?count=15&orderby=title&add_to_wishlist=4846 HTTP/1.1" 404 10202 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"""
    result = parser(logLine)
    print(result)
    

    RESULT

    ('180.76.15.30', '[24/Mar/2017:19:37:57 +0000]', 'GET /shop/page/32/?count=15&orderby=title&add_to_wishlist=4846 HTTP/1.1', '404', '10202')
    
    0 讨论(0)
提交回复
热议问题