Skip to main content
 首页 » 编程设计

python之正则表达式重复模式

2024年08月26日30java哥

我尝试使用正则表达式从下面的日志中捕获数据组。模式是

<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,>>RATIO: casher#=30, Value=2.579,Units=ratio,Error=N 20141207,07:15:52,0,>>RATIO: casher#=31, Value=4.509,Units=ratio,Error=N 20141207,07:15:52,0,>>RATIO: casher#=32, Value=3.735,Units=ratio,Error=N 20141207,07:15:52,0,>>RATIO: casher#=33, Value=2.401,Units=ratio,Error=N

20141207,07:15:52,0,>>CUSTOMER: casher#=30, Value=50,Units= count 20141207,07:15:52,0,>>CUSTOMER: casher#=31, Value=6,Units= count 20141207,07:15:52,0,>>CUSTOMER: casher#=32, Value=88,Units= count 20141207,07:15:52,0,>>CUSTOMER: casher#=33, Value=33,Units= count

显然结果不是预期的那样。谁能给我一些提示?我最终使用 python 转换为代码。谢谢。

请您参考如下方法:

(?<=>>)(\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