我尝试使用正则表达式从下面的日志中捕获数据组。模式是
<item> : <key> = <value> , <key> = <value>, ..., <key> = <value>
([#\w\d]*?)[\s]*=[\s]*([.\w\d]*)
可以捕获论坛<key>
和论坛<value>
但我也希望捕获<item>
组,因此我将上述组合并重复使用{n}。
([\w]*):([\s]*(([#\w\d]*?)[\s]*=[\s]*([.\w\d]*)),*){1,}
20141207,07:15:52,0,&gt;&gt;比例:casher#= 30, 值= 2.579,单位=比率,误差= N 20141207,07:15:52,0,&gt;&gt;比率: casher#= 31,Value = 4.509,Units = ratio,Error = N. 20141207,07:15:52,0,&gt;&gt; RATIO:casher#= 32, 值= 3.735,单位=比率,误差= N 20141207,07:15:52,0,&gt;&gt;比率: casher#= 33,Value = 2.401,Units = ratio,Error = N
20141207,07:15:52,0,&gt;&gt; CUSTOMER:casher#= 30,Value = 50,Units = count 20141207,07:15:52,0,&gt;&gt; CUSTOMER:casher#= 31,Value = 6,Units = count 20141207,07:15:52,0,&gt;&gt; CUSTOMER:casher#= 32,Value = 88,Units = count 20141207,07:15:52,0,&gt;&gt; CUSTOMER:casher#= 33,Value = 33,Units = count
显然结果不是预期的结果。任何人都可以给我一些提示吗?我最终使用python转换为代码。谢谢。
答案 0 :(得分:2)
(?<=>>)(\w+):|([\w#]+)\s*=\s*(\S+?)(?:,|\s)
试试这个。抓住捕获。参见演示。
https://regex101.com/r/fA6wE2/1
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
>> '>>'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[\w#]+ any character of: word characters (a-z,
A-Z, 0-9, _), '#' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\S+? non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
least amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of grouping
答案 1 :(得分:0)
您的文件是csv文件,因此您可以让您的生活更轻松并使用csv模块:
import csv
f = open('data.txt', 'rb')
for row in csv.reader(f, delimiter=','):
if row:
item, key_and_val = row[3].split(':')
item = item[2:]
key, val = key_and_val.split('=')
print item
print ' {} => {}'.format(key.strip(), val.strip())
for key_and_val in row[4:]:
key, val = key_and_val.split('=')
print ' {} => {}'.format(key.strip(), val.strip())
--output:--
RATIO
casher# => 30
Value => 2.579
Units => ratio
Error => N
RATIO
casher# => 31
Value => 4.509
Units => ratio
Error => N
RATIO
casher# => 32
Value => 3.735
Units => ratio
Error => N
RATIO
casher# => 33
Value => 2.401
Units => ratio
Error => N
CUSTOMER
casher# => 30
Value => 50
Units => count
CUSTOMER
casher# => 31
Value => 6
Units => count
CUSTOMER
casher# => 32
Value => 88
Units => count
CUSTOMER
casher# => 33
Value => 33
Units => count
你的匹配模式也匹配key = value,即使&#34;项目:&#34;不 存在,排除那些键=值行的任何先进技术?
以下内容将跳过没有项目的行:
for row in csv.reader(f, delimiter=','):
if row:
if row[3].startswith('>>'): #Check if there is an item
item, key_and_val = row[3].split(': ')
item = item[2:]
key, val = key_and_val.split('=')
print item
print ' {} => {}'.format(key.strip(), val.strip())
for key_and_val in row[4:]:
key, val = key_and_val.split('=')
print ' {} => {}'.format(key.strip(), val.strip())
f.close()