I want to do the following match:
match if
MBzzis in the string but not if[Rr][Ee][Ff]is in the string
So the following should match:
And the following should not match:
etc.
For now, I am doing this terrible hack:
def mySearch(val):
if (re.compile('MBab').search(val) is not None) and \
(re.compile('[Rr][Ee][Ff]').search(val) is None):
return re.compile('MBab').search(val).group()
return None
However, I feel that for something that is as simple as this, I should be able to accomplish this as a one liner.
You can use following regex with modifier i for ignoring the case :
^(?:(?!ref).)*(?=MBzz)(?:(?!ref).)*$
Demo
regex=re.compile(r'^[^ref]*(?=MBzz)[^ref]*$',re.I|re.MULTILINE)
The positive look behind (?=MBzz) will ensure your regex engine that your string contains MBzz and following negative look behind (?:(?!ref).)* will match any thing except ref.
And if you want yo consider the case for MBzz you can use following regex without ignore case modifier :
^(?:(?![rR][eE][fF]).)*(?=MBzz)(?:(?![rR][eE][fF]).)*$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With