The following should work:
^([AZ])(?!.?\1)([AZ])(?!\2)([AZ])[az]\1\1\3$
For instance:
>>> regex = re.compile(r'^([AZ])(?!.?\1)([AZ])(?!\2)([AZ])[az]\1\1\3$') >>> regex.match('ABAaAAA')
Explanation:
^
If you want to pull these matches from a larger piece of text, just get rid of ^ and $ and use regex.search() or regex.findall() .
However, you can more easily understand the following approach, it uses a regular expression for basic validation, but then uses regular string operations to validate all additional requirements:
def validate(s): return (re.match(r'^[AZ]{3}[az][AZ]{3}$', s) and s[4] == s[0] and s[5] == s[0] and s[-1] == s[2] and len(set(s[:3])) == 3) >>> validate('ABAaAAA') False >>> validate('ABCaABC') False >>> validate('ABCaAAB') False >>> validate('ABCaAAC') True
source share