It looks like you can use MAN(?!.*PN)
. This matches MAN
and uses negative lookahead to make sure that it's not followed by PN
(as seen on rubular.com).
Given MAN_PN_MAN_BLEH
, the above pattern will find the second MAN
, since it's not followed by PN
. If you want to validate the entire string and make sure that there's no MAN.*PN
, then you can use something like ^(?!.*MAN.*PN).*MAN.*$
(as seen on rubular.com).
References
Related questions
Non-regex option
If the strings are to be matched literally, then you can also check for indices of substring occurrences.
In Python, find
and rfind
return lowest and highest index of substring occurrences respectively.
So to make sure that string1
occurs but never followed by string2
, and both returns -1
if the string is not found, so it looks like you can just test for this condition:
string.rfind(s, string2) < string.find(s, string1)
This compares the leftmost occurrence of string1
and the rightmost occurrence of string2
.
- If neither occurs, both are
-1
, and result is false
- If
string1
occurs, but string2
doesn't, then result is true
as expected
- If both occurs, then the rightmost
string2
must be to the left of the leftmost string1
- That is, no
string1
is ever followed by string2
API links