0

I'm trying to run the following regexp:

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

It works as intended. For the sake of readability, I decided to space it out since it is a multiline:

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

When I run the following code (which worked with the one-line version):

why = password_regexp.search(password)
why.group()

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

Why? The one-line version worked, why would placing it on multiple lines with ''' ruin it?

1 Answers1

2

You need to use the re.VERBOSE flag and remove the extra parenthesis.

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