繁体   English   中英

Python 多行(多行)的正则表达式取决于第 1 行

[英]Python regular expressions over several rows (multiline) in depending of Row 1

我有一个具有以下结构的日志(txt 文件)。

At 2020-07-15 14:05:18 - Markers detected in this frame : 3 | 6 | 
ID :6 out of compartment G2A44

或者

At 2020-07-15 14:05:47 - Markers detected in this frame : 3 | 0 | 9 | 
ID :9 out of compartment G2A13
ID :9 out of compartment G2A45

正则表达式

我需要的信息

  1. 2020-07-15 (群1)
  2. 14:05:47(第二组)
  3. ID:9 (group4)
  4. G2A13...

当我At 2020-07-15 14:05:47 - Markers detected in this frame: 3 | 0 | 9 | At 2020-07-15 14:05:47 - Markers detected in this frame: 3 | 0 | 9 | 一切都将被表达式expr = 'At ([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) - Markers detected in this frame: ([0-9]{1,}.{1,})\s(ID..[0-9])\sout of compartment ([\w]{4,})'

但是如何在正则表达式中获得具有相同组匹配的第二行或第三行?

import re
expr = 'At ([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) - Markers detected in this frame : ([0-9]{1,} .{1,})\s(ID..[0-9])\sout of compartment ([\w]{4,})'
f = 'XX.txt'
file = open(f,'r')
text = file.read()
m = []
m = re.findall(expr,text, re.MULTILINE)
print(m)

你要求一个解析器。 您需要一台 state 机器。

根据 header 表达式测试目标行,并存储一些值。 如果它没有通过该测试,则根据下一个表达式测试该行,并对新的匹配项和存储的值执行一些操作。

不要期望一次获得所有线路。 这是一个两阶段的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM