You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Regex提取客户请求日志信息的问题求助

日志信息提取问题解决

客户请求日志示例

req=['"Software   not   available   on  Software   Center,   when   tried   to   raise  AHO   for   required   software   it opens Software   Center   with   error   as  \'This   Software   is not   available   for  you\'    ,   Need   to   install   following software   for client   demo   urgently   -  nodejs,   intellij,   ,   angular,      mongo   db,  compass   ,   Java,  Open3DK   \n (from   10.61.107.166)   \n   Logged   through   #OneClickAHD#   \n   Contact:Ashutosh   Suresh   Mitkari,   STGPW\nCTZPSAPR   \n   Email:   Ashutosh_Mitkari@google.com"']

提取需求

需从日志中提取以下信息,并存入对应初始化空列表:

request_list = []
ip_address_list = []
text_between_hashes_list = []
contact_name_list = []
unit_list = []
email_list = []

具体规则:

  • 提取(from之前的所有文本,未匹配则存空字符串
  • 提取from后的IP地址(去除空格),未匹配则存空字符串
  • 提取# #之间的文本,未匹配则存空字符串
  • 提取contact:之后到,之前的姓名,未匹配则存空字符串
  • 提取Contact:Ashutosh Suresh Mitkari,之后的单位信息(预期结果:STGPW\nCTZPSAPR),未匹配则存空字符串
  • 提取Email:之后的邮箱地址,未匹配则存空字符串

原尝试代码(未得到预期结果)

import re

for req in ahd_req:
    # extract till first \n
    match = re.search(r'^(.*?)\n', req)
    if match:
        print(match.group(1))
    
    # extract IP address after 'from'
    match = re.search(r'from\s+([\d\.]+)', req)
    if match:
        print(match.group(1))
        
    # extract text between # #
    match = re.search(r'#(.*?)#', req)
    if match:
        print(match.group(1))
    
    # extract name after 'contact:' till ,
    match = re.search(r'Contact:([^,]*),', req)
    if match:
        print(match.group(1))
    
    # extract unit after Contact:Ramesh Najukrao Sangle,` till \n\n
    match = re.search(r'Contact:.*?,\s*(.*?)\n\n', req)
    if match:
        print(match.group(1))

问题分析与修正代码

原代码存在以下问题:

  1. 提取(from前文本时,错误用\n作为分隔符,未匹配到(from的终止位置
  2. 提取单位信息时,错误用\n\n作为结束标记,实际日志中单位后是单个\n
  3. 未处理邮箱提取逻辑,部分正则未考虑大小写和多余空格
  4. 仅打印结果,未存入指定列表

修正后的代码:

import re

# 初始化目标列表
request_list = []
ip_address_list = []
text_between_hashes_list = []
contact_name_list = []
unit_list = []
email_list = []

# 示例日志数据
ahd_req = ['"Software   not   available   on  Software   Center,   when   tried   to   raise  AHO   for   required   software   it opens Software   Center   with   error   as  \'This   Software   is not   available   for  you\'    ,   Need   to   install   following software   for client   demo   urgently   -  nodejs,   intellij,   ,   angular,      mongo   db,  compass   ,   Java,  Open3DK   \n (from   10.61.107.166)   \n   Logged   through   #OneClickAHD#   \n   Contact:Ashutosh   Suresh   Mitkari,   STGPW\nCTZPSAPR   \n   Email:   Ashutosh_Mitkari@google.com"']

for req in ahd_req:
    # 1. 提取(from之前的文本
    match = re.search(r'^(.*?)\s*\(from', req)
    request_list.append(match.group(1).strip() if match else '')
    
    # 2. 提取from后的IP地址
    match = re.search(r'from\s+([\d\.]+)', req)
    ip_address_list.append(match.group(1).strip() if match else '')
    
    # 3. 提取#之间的文本
    match = re.search(r'#(.*?)#', req)
    text_between_hashes_list.append(match.group(1).strip() if match else '')
    
    # 4. 提取contact到,之间的姓名
    match = re.search(r'Contact:\s*([^,]+),', req, re.IGNORECASE)
    if match:
        name = ' '.join(match.group(1).strip().split())
        contact_name_list.append(name)
    else:
        contact_name_list.append('')
    
    # 5. 提取单位信息
    match = re.search(r'Contact:.*?,\s*(.*?)\s*\n\s*Email:', req, re.DOTALL)
    unit_list.append(match.group(1).strip() if match else '')
    
    # 6. 提取邮箱地址
    match = re.search(r'Email:\s*([\w\.-]+@[\w\.-]+)', req, re.IGNORECASE)
    email_list.append(match.group(1).strip() if match else '')

# 验证结果
print("请求内容:", request_list)
print("IP地址:", ip_address_list)
print("#之间文本:", text_between_hashes_list)
print("联系人姓名:", contact_name_list)
print("单位信息:", unit_list)
print("邮箱:", email_list)

代码说明

  • 提取(from前文本:用\s*\(from作为终止匹配符,确保准确截取请求内容
  • 提取姓名:用re.IGNORECASE兼容大小写,' '.join(name.split())合并姓名中的多余空格
  • 提取单位:用re.DOTALL让.匹配换行符,确保捕获包含换行的单位信息
  • 提取邮箱:使用标准邮箱正则匹配,兼容大小写
  • 所有结果均存入对应列表,未匹配时存入空字符串

内容的提问来源于stack exchange,提问作者Nishant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 03:45:03