Home > Mobile >  Regex to find combination of lines not containing a string
Regex to find combination of lines not containing a string

Time:10-14

I'm trying to find the correct regex that, within this input:

@Tag-1234
Scenario:
Blabla

Scenario:
Blabla

@Tag-1234
Scenario:
Blabla

Will select only the second one (the one without a tag). So far I tried something like (?!@Tag-\d{4})\nScenario, but it's not doing the trick.

Can anyone throw some light into this?

I'm doing my tests on regex101 -> https://regex101.com/r/msDHKf/1

Thanks

CodePudding user response:

It seems I was missing the \n before the tag that would concatenate to the search, so the regex would be

\n(?!@Tag-\d{4})\nScenario

So close!

CodePudding user response:

In the pattern (?!@Tag-\d{4})\nScenario the lookahead can be removed as it is directly followed by matching \nScenario so the assertion will always be true.

If there should be no tag before Scenario but a newline, you can just match 2 newlines and then Scenario

\n\nScenario\b

See a regex demo.

  • Related