Regex – match until a group of multiple possibilities

Tags: , ,



I have the following text:
You may have that thing NO you dont BUT maybe yes

I’m trying to write a regex which can match everything until it finds some specific words, “NO” and “BUT” in this example, and if the string has both of the words, then stop at the first one:

You may have that thing NO you dont BUT maybe yes
You may have that thing

You may have that thing you dont BUT maybe yes
You may have that thing you dont

I was trying the regex below, but the problem is that it stops at BUT even when it has NO:

(.*)(?:NO|BUT)

Match example of the above regex, in bold being the full match and in italic being group 1:
You may have that thing NO you dont BUT maybe yes

What i expect:
You may have that thing NO you dont BUT maybe yes

Answer

Let us fix your regex pattern

^(.*?)s*(?:NO|BUT)

Now we can use the above regex pattern with search

s = 'You may have that thing NO you dont BUT maybe yes'
match = re.search(r'^(.*?)s*(?:NO|BUT)', s)

>>> match.group(1)
'You may have that thing'

Regex details:

  • ^ : Assert position at the start of line
  • (.*?) : First capturing group
    • .*? : Matches any character zero or more times but as few times as possible
  • s* : Zero or more whitespace characters
  • (?:NO|BUT) : Non capturing group
    • NO|BUT : Matches one of NO, BUT

See the online regex demo



Source: stackoverflow