Python – how to include a boolean AND within a regex

python, regex

Is there a way to get single regex to satisfy this condition??

I am looking for a "word" that has three letters from the set MBIPI, any order,
but MUST contain an I.


re.match("[MBDPI]{3}", foo) and "I" in foo

So this is the correct result (in python using the re module), but can I get this from a single regex?

>>> for foo in ("MBI", "MIB", "BIM", "BMI", "IBM", "IMB", "MBD"):...     print foo,...     print re.match("[MBDPI]{3}", foo) and "I" in fooMBI TrueMIB TrueBIM TrueBMI TrueIBM TrueIMB TrueMBD False

with regex I know I can use | as a boolean OR operator, but is there a boolean AND equivalent?

or maybe I need some forward or backward lookup?

Best Solution

You can fake boolean AND by using lookaheads. According to, this will work for your case: