简体   繁体   English

正则表达式捕获部分文本,但如果找到模式则忽略 rest

[英]Regexp capture part of text but ignores rest if patern found

Using regexp I need to "filter" some text but only if part of line matches pattern.使用正则表达式我需要“过滤”一些文本,但前提是部分行匹配模式。

Sample input is (each line is separate record):示例输入是(每行是单独的记录):

OK: ALL OK (8536972.66889)
ERROR: ioerror/8536615.22927
OK: ALL OK (8546369.92291)

In case "OK: ALL OK" I need filter out (float) in other cases all line should match.如果"OK: ALL OK" ,我需要过滤掉(浮动),在其他情况下,所有行都应该匹配。 Every match should be in "the same capture group" (the same means always ex in 4 capture group).每场比赛都应该在“同一个捕获组”中(相同意味着总是在 4 个捕获组中)。 So correct output should be所以正确的 output 应该是

OK: ALL OK
OK: ALL OK
ERROR: ioerror/8536615.22927
OK: ALL OK

I've tried: ((OK: ALL OK) (?:\(.*\))|ERROR: .*)我试过了: ((OK: ALL OK) (?:\(.*\))|ERROR: .*)

and got result:并得到结果:

Match 1:
Group 1: OK: ALL OK (8536972.66889)
Group 2: OK: ALL OK

Match 2:
Group1: ERROR: ioerror/8536615.22927

I need "OK: ALL OK" or "ERROR: ioerror/8536615.22927" always in the same capture group, any ideas how to do these?我需要始终在同一个捕获组中的"OK: ALL OK""ERROR: ioerror/8536615.22927" ,有什么办法吗?

If you want the matches all in group 1, you can use a branch reset group as the outer group, and put the ERROR part in it's own group:如果你想要第 1 组中的所有匹配项,你可以使用分支重置组作为外部组,并将 ERROR 部分放在它自己的组中:

(?|(OK: ALL OK) (?:\(.*\))|(ERROR: .*))

Regex demo正则表达式演示

Al alternative without capture groups and using a lookahead assertion to assert a (...) part after matching OK: ALL OK Al alternative without capture groups and using a lookahead assertion to assert a (...) part after matching OK: ALL OK

\bERROR:\h.*|\bOK:\hALL\hOK(?=\h+\([^()]*\))

Regex demo正则表达式演示

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

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