微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python请求的正则表达式

如何解决python请求的正则表达式

嗨,我正在寻找一种创建函数解决方案,该函数可返回带有下一个结构的词典列表

示例

example_dict = {"host":"146.204.224.152","user_name":"feest6811","time":"21/Jun/2019:15:45:24 -0700","request":"POST /incentivize HTTP/1.1"}

数据如下:

146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554 
156.127.178.177 - okuneva5222 [21/Jun/2019:15:45:27 -0700] "DELETE /interactive/transparent/niches/revolutionize HTTP/1.1" 416 14701
*Keeps going more entries...*

我的功能如下:

import re
def logs():
    with open("assets/logdata.txt","r") as file:
        logdata = file.read()
    pattern="""
(?P<host>.[\d.]*\s?)         #host
(?P<user_name>[\s\w-]*\s?)    #user_name
(?P<time>[\w\/\:\.\[\s-]*[\]\s])           #time
(?P<request>[\w\/\"\s.]*"?)     #request"""
    group=[]
    for item in re.finditer(pattern,logdata,re.VERBOSE):
        group.append(item.groupdict())
    return group    
    raise NotImplementedError()

然后重新调谐如下内容

[{'host': '146.204.224.152 ','user_name': '- feest6811 ','time': '[21/Jun/2019:15:45:24 -0700]','request': ' "POST /incentivize HTTP/1.1" 302 4622\n197.109.77.178 '},{'host': '- ','user_name': 'kertzmann3129 ','time': '[21/Jun/2019:15:45:25 -0700]','request': ' "DELETE /virtual/solutions/target/web'},{'host': '+','user_name': 'services','time': ' ','request': 'HTTP/2.0" 203 26554\n156.127.178.177 '}]

我可以更改些什么以解决错误

解决方法

您可以尝试使用正则表达式。

(?P<host>[\d.]+)(?:\s*-\s*)(?P<user_name>\w+)(?:\s*\[)(?P<time>.*?)(?:\])(?:\s*)(?P<request>\".*?\")

Demo

,

尝试一下:

    pattern="""
    (?P<host>\d{1,3}(?:\.\d{1,3}){3})\s-\s  #host (IPv4 only)
    (?P<user_name>[\s\w-]*)\s?              #user_name
    \[(?P<time>[\w\/\:\.\s-]*)\]\s?         #time
    "(?P<request>.*?)"\s?                   #request
    (?P<code>\d{3})\s?                      #response code
    (?P<bytes>\d+)\s?                       #bytes sent or received
    """

https://regex101.com/r/m9upkt/3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。