密码要求的正则表达式 [英] Regex for password requirements

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

问题描述

我想要求以下内容:

  • 大于七个字符.
  • 至少包含两位数字.
  • 至少包含两个特殊(非字母数字)字符.

...我想出了这个来做到这一点:

...and I came up with this to do it:

(?=.{6,})(?=(.*\d){2,})(?=(.*\W){2,})

现在,我还想确保没有两个连续的字符是相同的.不过,我花了很长时间才能让它发挥作用.这是我自己得到的:

Now, I'd also like to make sure that no two sequential characters are the same. I'm having a heck of a time getting that to work though. Here's what I got that works by itself:

(\S)\1+

...但是如果我尝试将两者结合在一起,它就会失败.

...but if I try to combine the two together, it fails.

我在应用程序的限制范围内操作.它的默认要求是 1 个字符长度,没有正则表达式,也没有非标准字符.

I'm operating within the constraints of the application. It's default requirement is 1 character length, no regex, and no nonstandard characters.

无论如何...

使用这个测试工具,我希望 y90e5$ 匹配但 y90e5$$ 不匹配.

Using this test harness, I would expect y90e5$ to match but y90e5$$ to not.

我错过了什么?

推荐答案

有时我们无法影响规范,必须无论如何都要编写实现,例如,当某些古老的后台系统必须通过 Web 连接但对输入有一定限制时,或者只是因为你的老板要求你这样做.

Sometimes we cannot influence specifications and have to write the implementation regardless, i.e., when some ancient backoffice system has to be interfaced through the web but has certain restrictions on input, or just because your boss is asking you to.

删除了基于提问者的原始正则表达式的正则表达式.

removed the regex that was based on the original regex of the asker.

更改了原始代码以符合您的描述,因为它似乎并没有真正起作用:
q.然后更新以反映另一个版本.我在下面解释了一些差异:

altered original code to fit your description, as it didn't seem to really work:
the q. was then updated to reflect another version. There are differences which I explain below:

我的版本:两个或多个 \W\d 可以互相重复,但不能相邻出现(这是我的错误假设),我将其固定为长度> 7,作为典型的全部抓取"表达式放置更有效.

My version: the two or more \W and \d can be repeated by each other, but cannot appear next to each other (this was my incorrect assumption), i fixed it for length>7 which is slightly more efficient to place as a typical "grab all" expression.

 ^(?!.*((\S)\1|\s))(?=.*(\d.+){2,})(?=.*(\W.+){2,}).{8,}

原始问题中的新版本:允许两个或多个 \W\d 彼此相邻出现.该版本目前支持长度>=6,而不是如文中所述的长度>7.

New version in original question: the two or more \W and the \d are allowed to appear next to each other. This version currently support length>=6, not length>7 as is explained in the text.

当前的答案,更正后,应该是这样的,它采用更新后的 q.,我对长度>7 和优化的评论,然后它看起来像:^(?!.*((\S)\1|\s))(?=(.*\d){2,})(?=(.*\W){2,}).{8,}.

The current answer, corrected, should be something like this, which takes the updated q., my comments on length>7 and optimizations, then it looks like: ^(?!.*((\S)\1|\s))(?=(.*\d){2,})(?=(.*\W){2,}).{8,}.

更新:您的原始代码似乎不起作用,因此我对其进行了一些更改
更新:更新答案以反映问题的变化,不再允许空格

Update: your original code doesn't seem to work, so I changed it a bit
Update: updated answer to reflect changes in question, spaces not allowed anymore

这篇关于密码要求的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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