RegEx 确保字符串至少包含一个小写字符、大写字符、数字和符号 [英] RegEx to make sure that the string contains at least one lower case char, upper case char, digit and symbol

查看:45
本文介绍了RegEx 确保字符串至少包含一个小写字符、大写字符、数字和符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确保给定字符串至少包含来自以下每个类别的一个字符的正则表达式是什么.

What is the regex to make sure that a given string contains at least one character from each of the following categories.

  • 小写字符
  • 大写字符
  • 数字
  • 符号

我知道单个集合的模式,即 [az][AZ]\d_|[^\w] (我猜对了,不是吗?).

I know the patterns for individual sets namely [a-z], [A-Z], \d and _|[^\w] (I got them correct, didn't I?).

但是我如何组合它们以确保字符串以任何顺序包含所有这些?

But how do I combine them to make sure that the string contains all of these in any order?

推荐答案

如果您需要一个正则表达式,请尝试:

If you need one single regex, try:

(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)

简短说明:

(?=.*[a-z])        // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z])        // use positive look ahead to see if at least one upper case letter exists
(?=.*\d)           // use positive look ahead to see if at least one digit exists
(?=.*\W])        // use positive look ahead to see if at least one non-word character exists

我同意 SilentGhost,\W 可能有点宽泛.我会用这样的字符集替换它:[-+_!@#$%^&*.,?](当然可以随意添加更多!)

And I agree with SilentGhost, \W might be a bit broad. I'd replace it with a character set like this: [-+_!@#$%^&*.,?] (feel free to add more of course!)

这篇关于RegEx 确保字符串至少包含一个小写字符、大写字符、数字和符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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