以任何顺序匹配正则表达式 [英] Match regex in any order

查看:122
本文介绍了以任何顺序匹配正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用正则表达式检查复杂的密码.

I want to check for complex password with regular expression.

它应该具有1个数字1个大写字母和一个小写字母,而不是特定的顺序. 所以我虽然这样:

It should have 1 number 1 uppercase and one lowercase letter, not in specific order. So i though about something like this:

m = re.search(r"([a-z])([A-Z])(\d)", "1Az")
print(m.group())

但是我不知道如何告诉他以任何顺序进行搜索. 我尝试浏览网络,但没有找到有趣的东西,谢谢您的帮助.

But i don't know how to tell him to search in any order. I tried to look on web but I didn't found something interesting, thanks for help.

推荐答案

您本可以尝试查找用于验证正则表达式的密码,该站点上有很多;)

You could have tried looking for password validating regex, the site has a lot of them ;)

也就是说,您可以使用积极前瞻来做到这一点:

That said, you can use positive lookaheads to do that:

re.search(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)", "1Az")

实际上要匹配字符串...

And to actually match the string...

re.search(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{3}", "1Az")

现在,确保密码长度为3个字符:

And now, to ensure that the password is 3 chars long:

re.search(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{3}$", "1Az")

正向超前(?= ... )确保要测试的字符串中存在表达式.因此,字符串必须具有小写字符((?=.*[a-z])),大写字符((?=.*[A-Z]))和数字((?=.*\d)),以使正则表达式能够通过".

A positive lookahead (?= ... ) makes sure the expression inside is present in the string to be tested. So, the string has to have a lowercase character ((?=.*[a-z])), an uppercase character ((?=.*[A-Z])) and a digit ((?=.*\d)) for the regex to 'pass'.

这篇关于以任何顺序匹配正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆