在日志文件中使用此模式
event y:
event x: specific data A
event y:
event z: count = 1 (or 2, 3, etc)
event y:
event x: specific data B
event y:
event z: count = 0
event y:
x y z表示的事件名称是静态的。
我想提取“count = 0”之前出现的“特定数据”。它足够接近我提取这些线。
event x: specific data B
event y:
event z: count = 0
我能做的最好的事情是(在editpad pro中使用多行选项)
event x.+?count = 0
但这给了我太多了
event x: specific data A
event y:
event z: count = 1 (or 2, 3, etc)
event y:
event x: specific data B
event y:
event z: count = 0
即使它非贪婪,但这场比赛还是“太过分了”
我怎样才能得到以下几行?
event x: specific data B
event y:
event z: count = 0
答案 0 :(得分:2)
你需要更明确,例如:
event x:(?>[^ec]++|\B[ec]|e(?!vent x:)|c(?!ount = 0))++count = 0
模式细节:
event x:
(?> # open an atomic group
[^ec]++ # all characters except e and c one or more times
| # OR
\B[ec] # e or c not precedent by a word boundary
| # OR
e(?!vent x:) # e not followed by "vent x:"
| # OR
c(?!ount = 0) # c not followed by "ount = 0"
)++ # repeat the atomic group one or more times
count = 0
答案 1 :(得分:2)
如果使用grep
是一个选项,它有一个-B n
参数,告诉它在匹配你给它的字符串/表达式的行之前包含n
行。所以grep -B 2 "count = 0"
应该这样做。
或者,如果您只想使用正则表达式,请尝试以下方法:
(?:^.*$\s){2}^.*count = 0
这可以分为两部分:(?:^.*$\s){2}
和^.*count = 0
第二部分显然是“包含'count = 0'的行”的正则表达式。
第一部分是“在此前包括两行”的正则表达式,其中^.*$\s
是“一行”的正则表达式。 (具体来说,一行的开头,后跟一行前的任意数量的字符和一个空白字符(必然是\n
)。