有什么方法可以知道 re.fullmatch 中的哪个条件不满足?

Is there any way to known which condition in re.fullmatch wasn't met?

我需要检查字符串是否满足条件:

我有:

if not re.fullmatch(r'^(?=.*[A-Z])(?=.*[a-z]).{4,}$', 'ab7c'):
   print('The string is wrong')

我可以知道哪些条件不满足吗? 在我的示例中,它们是:

好吧,您可以单独执行每个断言:

def checkString(input):
    msg = ''
    if len(input) < 4:
        msg = '  -length must be 4 or more characters'
    if re.search(r'[^A-Za-z]', input):
        msg = msg + '\n  -input must consist of only letters'
    if not re.search(r'[A-Z]', input):
        msg = msg + '\n  -input must have at least one uppercase letter'

    if msg:
        print('The input fails validation:\n  ' + msg.strip())

checkString('ab7')

这会打印:

The input fails validation:
  -length must be 4 or more characters
  -input must consist of only letters
  -input must have at least one uppercase letter

但是,如果输入只缺少一个标准,那么我们会得到:

checkString('ABC3')

The input fails validation:
  -input must consist of only letters