如何检查是否具有多个正则表达式的字符串 fullfil 并捕获匹配的那部分?
how to check if a string fullfil with multiple regex and capture that portion that match?
我想要的
我正在使用 django
表单,它需要输入密码。我需要为多个正则表达式传递输入值,这将测试是否:
- 至少有一个字符是小写字母
- 至少有一个字符是大写
- 至少有一个字符是数字
- 至少有一个字符是特殊字符(符号)
- 最少 8 个字符
而且我想知道这些条件中哪些已经满足,哪些没有。
我做了什么
def clean_password(self):
password = self.cleaned_data.get("password")
regexes = [
"[a-z]",
"[A-Z]",
"[0-9]",
#other regex...
]
# Make a regex that matches if any of our regexes match.
combined = "(" + ")|(".join(regexes) + ")"
if not re.match(combined, password):
print("Some regex matched!")
# i need to pass in ValidationError those regex that haven't match
raise forms.ValidationError('This password does not contain at least one number.')
虽然你可以在这里使用正则表达式,但我会坚持使用正常 Python:
from string import ascii_uppercase, ascii_lowercase, digits, punctuation
from pprint import pprint
character_classes = {'lowercase letter': ascii_lowercase,
'uppercase letter': ascii_uppercase,
'number': digits,
'special character': punctuation # change this if your idea of "special" characters is different
}
minimum_length = 8
def check_password(password):
long_enough = len(password) >= minimum_length
if not long_enough:
print(f'Your password needs to be at least {minimum_length} characters long!')
result = [{class_name: char in char_class for class_name, char_class in character_classes.items()} for char in password]
result_transposed = {class_name: [row[class_name] for row in result] for class_name in character_classes}
for char_class, values in result_transposed.items():
if not any(values):
# Instead of a print, you should raise a ValidationError here
print(f'Your password needs to have at least one {char_class}!')
return result_transposed
check_password('12j3dSe')
输出:
Your password needs to be at least 8 characters long!
Your password needs to have at least one special character!
这允许您以更灵活的方式修改密码要求,以防万一您想说 "you need X of this character class"...
我想要的
我正在使用 django
表单,它需要输入密码。我需要为多个正则表达式传递输入值,这将测试是否:
- 至少有一个字符是小写字母
- 至少有一个字符是大写
- 至少有一个字符是数字
- 至少有一个字符是特殊字符(符号)
- 最少 8 个字符
而且我想知道这些条件中哪些已经满足,哪些没有。
我做了什么
def clean_password(self):
password = self.cleaned_data.get("password")
regexes = [
"[a-z]",
"[A-Z]",
"[0-9]",
#other regex...
]
# Make a regex that matches if any of our regexes match.
combined = "(" + ")|(".join(regexes) + ")"
if not re.match(combined, password):
print("Some regex matched!")
# i need to pass in ValidationError those regex that haven't match
raise forms.ValidationError('This password does not contain at least one number.')
虽然你可以在这里使用正则表达式,但我会坚持使用正常 Python:
from string import ascii_uppercase, ascii_lowercase, digits, punctuation
from pprint import pprint
character_classes = {'lowercase letter': ascii_lowercase,
'uppercase letter': ascii_uppercase,
'number': digits,
'special character': punctuation # change this if your idea of "special" characters is different
}
minimum_length = 8
def check_password(password):
long_enough = len(password) >= minimum_length
if not long_enough:
print(f'Your password needs to be at least {minimum_length} characters long!')
result = [{class_name: char in char_class for class_name, char_class in character_classes.items()} for char in password]
result_transposed = {class_name: [row[class_name] for row in result] for class_name in character_classes}
for char_class, values in result_transposed.items():
if not any(values):
# Instead of a print, you should raise a ValidationError here
print(f'Your password needs to have at least one {char_class}!')
return result_transposed
check_password('12j3dSe')
输出:
Your password needs to be at least 8 characters long!
Your password needs to have at least one special character!
这允许您以更灵活的方式修改密码要求,以防万一您想说 "you need X of this character class"...