RegEx 确保字符串至少包含一个小写字符、大写字符、数字和符号

编程入门 行业动态 更新时间:2024-10-27 20:31:22
本文介绍了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 确保字符串至少包含一个小写字符、大写字符、数字和符号

本文发布于:2023-11-12 11:30:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   字符串   符号   数字   RegEx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!