16

Can someone give an idea on how to test a string that:

  • contains at least one upper case letter
  • contains at least one lower case letter
  • contains at least one number
  • has a minimal length of 7 characters
4

2 回答 2

67
if (any(x.isupper() for x in s) and any(x.islower() for x in s) 
    and any(x.isdigit() for x in s) and len(s) >= 7):

另一种方法是将您的规则表达为 (lambda) 函数的列表

rules = [lambda s: any(x.isupper() for x in s), # must have at least one uppercase
        lambda s: any(x.islower() for x in s),  # must have at least one lowercase
        lambda s: any(x.isdigit() for x in s),  # must have at least one digit
        lambda s: len(s) >= 7                   # must be at least 7 characters
        ]

if all(rule(s) for rule in rules):
    ...

关于你的评论。构建错误消息

errors = []
if not any(x.isupper() for x in password):
    errors.append('Your password needs at least 1 capital.')
if not any(x.islower() for x in password):
    errors.append(...)
...

if errors:
    print " ".join(errors)
于 2013-06-17T04:34:22.053 回答
9
import re

s   = 'fooBar3'
rgx = re.compile(r'\d.*?[A-Z].*?[a-z]')

if rgx.match(''.join(sorted(s))) and len(s) >= 7:
    print 'ok'

更有趣的是这个正则表达式,它将报告丢失的字符类型:

s = 'fooBar'

rules = [
    r'(?P<digit>\d)?',
    r'(?P<upper>[A-Z])?',
    r'(?P<lower>[a-z])?',
]

rgx      = re.compile(r'.*?'.join(rules))
checks   = rgx.match(''.join(sorted(s))).groupdict()
problems = [k for k,v in checks.iteritems() if v is None]

print checks   # {'upper': 'B', 'digit': None, 'lower': 'a'}
print problems # ['digit']

最后,这里是 gnibbler 建议的基于规则的优秀方法的一个变体。

s = 'fooBar'

rules = [
    lambda s: any(x.isupper() for x in s) or 'upper',
    lambda s: any(x.islower() for x in s) or 'lower',
    lambda s: any(x.isdigit() for x in s) or 'digit',
    lambda s: len(s) >= 7                 or 'length',
]

problems = [p for p in [r(s) for r in rules] if p != True]

print problems  # ['digit', 'length']
于 2013-06-17T04:44:57.087 回答