-1

I'm trying to match a pattern that starts with "START", ends with "END" unless it contains "NOPE_1" or "NOPE_2"

The result I'm going for is :

xxxxxxxxxSTARTxxxxxxxxxxxxxxxxxxxENDxxxxxxxxx -> match
xNOPE_1xxSTARTxxxxxxxxxxxxxxxxxxxENDxxxxxxxxx -> match
xxxxxxxxxSTARTxxxxxxxxxxxxxxxxxxxENDxxNOPE_2x -> match
xxxxxxxxxSTARTxxxxxxxxNOPE_1xxxxxENDxxxxxxxxx -> no match
xxxxxxxxxSTARTxxxxxxxxNOPE_2xxxxxENDxxxxxxxxx -> no match

I tried using lookahead/behind but didn't succeed..

I am using python and re

Thanks for any help !

Hugo Flick
  • 99
  • 1
  • 3

2 Answers2

1

You may use this regex with a negative lookahead:

START((?!NOPE_[12]).)*?END

RegEx Demo

This part ((?!NOPE_[12]).)*? matches 0 or more of any characters checking each character is not followed by NOPE_1 or _NOPE_2.

anubhava
  • 664,788
  • 59
  • 469
  • 547
0

I suggest that it would probably be simpler to use two different regex checks.

Have one regex that checks for START and END. If your string passes that check, then check the result of the first check for NOPE_1 and NOPE_2.

You can use lookaheads, but it will be easier to write and maintain if it's two different checks.

Andy Lester
  • 81,480
  • 12
  • 93
  • 144