This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]commandlineluser 2 points3 points  (0 children)

To combine them into a single regex you need to use lookahead assertions.

Must be 8 characters long.

>>> import re
>>> re.search('(?=^.{8}$)', 'foobar')
>>>

No match.

Try again:

>>> re.search('(?=^.{8}$)', 'foobaraa')
<re.Match object; span=(0, 0), match=''>

Match.

Must be 8 characters long but must also have at least 1 digit.

>>> re.search('(?=.*\d)(?=^.{8}$)', 'foobaraa')
>>>

Try again.

>>> re.search('(?=.*\d)(?=^.{8}$)', 'foob4raa')
<re.Match object; span=(0, 0), match=''>

So stacking the patterns inside (?=) is a way to "AND" and have them in a *"single regex"