Home > Software design >  Regexp capture part of text but ignores rest if patern found
Regexp capture part of text but ignores rest if patern found

Time:12-10

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. Every match should be in "the same capture group" (the same means always e.x. in 4 capture group). So correct output should be

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

I've tried: ((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?

CodePudding user response:

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:

(?|(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

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

Regex demo

  • Related