Regular expression to search for a string1 that is never followed by string2
How to construct a regular expression search pattern to find string1 that is not followed by string2 (immediately or not)?
For for instance, if string1="MAN" and string2="PN", example search results would be:
"M": Not found
"MA": Not found
"MAN": Found
"BLAH_MAN_BLEH": Found
"MAN_PN": Not found
"BLAH_MAN_BLEH_PN": Not found开发者_JS百科
"BLAH_MAN_BLEH_PN_MAN": Not found
Ideally, a one-linear search, instead of doing a second search for string2.
PS: Language being used is Python
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
- regular-expressios.info/Lookarounds
Related questions
- How can I check if every substring of four zeros is followed by at least four ones using regular expressions?
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 isfalse
- If
string1
occurs, butstring2
doesn't, then result istrue
as expected - If both occurs, then the rightmost
string2
must be to the left of the leftmoststring1
- That is, no
string1
is ever followed bystring2
- That is, no
API links
find
rfind
精彩评论