0

I have the two below lines as output

   1   ID_1    INSV  enabled   Activated   12.345.67.89
   2   ID_2    SYSB  Disabled  Activated    12.345.67.89

I want to match the lines that should NOT have SYSB but it should have Activated. I am writing the below regex

ID_\d+\s+(?!SYSB)\s+\w+\s+Activated

It ignores the SYSB line, but its not capturing INSV line too. How to solve this regex?

prasanna kumar
  • 257
  • 3
  • 4
  • 17
  • You can use `^(?!.* SYSB ).* Activated\b` – anubhava Sep 06 '19 at 07:19
  • Wouldn't it be easier to just check each line and use the `in`, `and`, `not` operators instead? – Sweeper Sep 06 '19 at 07:21
  • A compiled regex should perform better depending on the number of rows. – Adirio Sep 06 '19 at 07:22
  • 1
    While @anubhava answer works, I wanted to answer your question directly. Your regex is not matching cos it doesn't have anything that matches the word in place of SYSB. The corrected version could be: `ID_\d+\s+(?!SYSB)\w*\s+\w+\s+Activated` adding that \w* to match `INSV` in your example case. – Adirio Sep 06 '19 at 07:28

1 Answers1

1

You may use negative lookahead like this:

^(?!.* SYSB ).* Activated\b

RegEx Demo

RegEx Details:

  • ^: Start
  • (?!.* SYSB ): Negative lookahead to fail the match if " SYSB " is present anywhere in the line
  • .* Activated\b: Match word Activated anywhere in the line after a space. \b is for word boundary.
anubhava
  • 761,203
  • 64
  • 569
  • 643