Can';t在Python中使用多行构成Regexp

Can';t在Python中使用多行构成Regexp,python,regex,multiline,Python,Regex,Multiline,我正在尝试运行以下regexp: password_regexp = re.compile(r'''^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{6,}$''') 它按预期工作。为了可读性,我决定将其隔开,因为它是一个多行: password_regexp = re.compile(r'''( ^(?=.*[a-z]) (?=.*[A-Z]) (?=.*\d) .+.{6,}$ )''') 当我运行以下代码时(使用单行版本):

我正在尝试运行以下regexp:

password_regexp = re.compile(r'''^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{6,}$''')
它按预期工作。为了可读性,我决定将其隔开,因为它是一个多行:

password_regexp = re.compile(r'''(
    ^(?=.*[a-z])
    (?=.*[A-Z])
    (?=.*\d)
    .+.{6,}$
    )''')
当我运行以下代码时(使用单行版本):

我得到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“非类型”对象没有属性“组”

为什么??一行版本可以工作,为什么将它放在多行中并带有“”会破坏它?

您需要使用
re.VERBOSE
标志并删除额外的括号

password_regexp = re.compile(r'''
    ^(?=.*[a-z])
    (?=.*[A-Z])
    (?=.*\d)
    .+.{6,}$
''', re.VERBOSE)

您需要使用
re.VERBOSE
标志并删除额外的括号

password_regexp = re.compile(r'''
    ^(?=.*[a-z])
    (?=.*[A-Z])
    (?=.*\d)
    .+.{6,}$
''', re.VERBOSE)

字面意思是刚要删除,谢谢你的帮助!字面意思是刚要删除,谢谢你的帮助!