大写,小写和数字的正则表达式
我的任务是验证一个必须包含至少一个小写和大写字符的字符串以及至少一个数字。我不必检查此字符串的长度。
我正在尝试做这样的事情:
from re import match
regexp = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])"
string_to_validate = input("Write string with uppercase/lowercase characters and numbers.")
if not match(regexp, string_to_validate):
raive ValueError("You should use uppercase and lowercase characters with numbers in your string")
但是在我看来,为此目的有一个表达式比那个目的要好得多。老实说,我什至不知道表达开始时的符号'^'是什么。
My task is to validate a string that must contain at least one lowercase and uppercase character and at least one number. I don't have to check the length of this string.
I'm trying to do something like this:
from re import match
regexp = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])"
string_to_validate = input("Write string with uppercase/lowercase characters and numbers.")
if not match(regexp, string_to_validate):
raive ValueError("You should use uppercase and lowercase characters with numbers in your string")
But it seems to me that there's an expression for this purpose that is much better than that one. Honestly, I don't even know what the symbol '^' at the beginning of the expression is used for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将需求分解为单个正则是更可维护的,更可读性,并且使用
re.search
:It is more maintainable and more readable to break up the requirements into individual regexes, and to use
re.search
: