密码验证正则表达式 [英] Password validation regex

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

问题描述

我正在尝试获得一个执行以下操作的正则表达式:

I am trying to get one regular expression that does the following:

  1. 确保没有空格字符
  2. 最小长度为 8
  3. 确保至少有:
    • 一个非字母字符
    • 一个大写字符
    • 一个小写字符

我发现了这个正则表达式:

I found this regular expression:

((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!\s).{8,})

处理上面的第 2 点和第 3 点,但如何将第一个要求添加到上述正则表达式中?

which takes care of points 2 and 3 above, but how do I add the first requirement to the above regex expression?

我知道我可以做两种表达方式,一种是上面的,然后是

I know I can do two expressions the one above and then

\s

但我想将所有内容合而为一,我尝试做类似 ?!\s 的事情,但我无法让它工作.有什么想法吗?

but I'd like to have it all in one, I tried doing something like ?!\s but I couldn't get it to work. Any ideas?

推荐答案

^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\S{8,}$

应该可以.但是请注意,您只是在验证 ASCII 字母.Ä 不是满足您要求的字母吗?

should do. Be aware, though, that you're only validating ASCII letters. Is Ä not a letter for your requirements?

\S 表示除了空格之外的任何字符",因此通过使用它而不是点,并通过在字符串的开头和结尾锚定正则表达式,我们确保字符串不'不包含任何空格.

\S means "any character except whitespace", so by using this instead of the dot, and by anchoring the regex at the start and end of the string, we make sure that the string doesn't contain any whitespace.

我还删除了整个表达式周围不必要的括号.

I also removed the unnecessary parentheses around the entire expression.

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

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