我有一个日志文件,我正在试图分析。日志文件示例如下:
Oct 23 13:03:03.714012 prod1_xyz(RSVV)[201]: #msgtype=EVENT #server=Web/Dev@server1web #func=LKZ_WriteData ( line 2992 ) #rc=0 #msgid=XYZ0064 #reqid=0 #msg=Web Activity end (section 200, # SysD 1, Files 222, Bytes 343422089928, Errors 0, Aborted Files 0, Busy Files 0)
我想取出所有以散列开头的文本,并且有一个键和值。例如,#msgtype=EVENT。任何只有散列且没有“=”符号的文本都将被视为值
所以在上面的日志条目中,我想要一个如下所示的列表
#msgtype=EVENT
#server=Web/Dev@server1web
#func=LKZ_WriteData ( line 2992 )
#rc=0
#msgid=XYZ0064
#reqid=0
#msg=Web Activity end (section 200, # SysD 1, Files 222, Bytes 343422089928, Errors 0, Aborted Files 0, Busy Files 0) (Notice the hash present in the middle of the text)
我已经尝试了Python regex findall选项,但无法捕获所有数据
例如:
str='Oct 23 13:03:03.714012 prod1_xyz(RSVV)[201]: #msgtype=EVENT #server=Web/Dev@server1web #func=LKZ_WriteData ( line 2992 ) #rc=0 #msgid=XYZ0064 #reqid=0 #msg=Web Activity end (section 200, # SysD 1, Files 222, Bytes 343422089928, Errors 0, Aborted Files 0, Busy Files 0)'
z = re.findall("(#.+?=.+?)(:?#|$)",str)
print(z)
输出:
[('#msgtype=EVENT ', '#'), ('#func=LKZ_WriteData ( line 2992 ) ', '#'), ('#msgid=XYZ0064 ', '#'), ('#msg=Web Activity end (section 200, ', '#')]
(:?#|$)
是一个捕获组,它匹配一个可选的:
,然后是#
,或者字符串的结尾。因为re.findall
返回所有捕获的子字符串,所以结果是一个元组列表你需要
参见regex demo
正则表达式详细信息
#[^\s=]+
-#
然后是除空格和=
之外的任何1+字符=
-a=
字符.*?
-除换行符以外的任何0+字符,尽可能少(?=\s*#[^\s=]+=|$)
-最多(不包括)0+个空格,#
,1+个除空格和=
以外的字符,然后=
或字符串结尾李>提供:
相关问题 更多 >
编程相关推荐