用于检查 4 个不同字符组中的至少 3 个的正则表达式组中、字符、不同、正则表达式

2023-09-06 22:35:39 作者:暮而归

我正在尝试编写密码验证器.

I'm trying to write a password validator.

如何查看我提供的字符串是否包含至少 3 个不同的字符组?

How can I see if my supplied string contains at least 3 different character groups?

检查它们是否存在很容易——但至少有 3 个?

It's easy enough to check if they are existant or not ---but at least 3?

至少八 (8) 个字符

at least eight (8) characters

至少三个不同的字符组

大写字母

小写字母

数字

特殊字符 !@#$%&/=?_.,:;-

special characters !@#$%&/=?_.,:;-

(我正在使用 javascript 进行正则表达式)

(I'm using javascript for regex)

推荐答案

Python 正则表达式匹配两个字符之间的字符

只是为了学习 - 这种要求是否可以在纯正则表达式中实现?

Just to learn - would this kind of requirement be possible to implement in pure regex?

这将使它成为一个相当难以阅读(并因此维护!)的解决方案,但它是:

That'd make it a rather hard to read (and therefor maintain!) solution, but here it is:

(?mx)
^
(
  (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])                # must contain a-z, A-Z and 0-9
  |                                                # OR
  (?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&/=?_.,:;\-]) # must contain a-z, A-Z and special
  |                                                # OR
  (?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%&/=?_.,:;\-]) # must contain a-z, 0-9 and special
  |                                                # OR
  (?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%&/=?_.,:;\-]) # must contain A-Z, 0-9 and special
)
.{8,}                                              # at least 8 chars
$

一个(可怕的)Javascript 演示:

A (horrible) Javascript demo:

var pw = "aa$aa1aa";
if(pw.match(/^((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&/=?_.,:;\-])|(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%&/=?_.,:;\-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%&/=?_.,:;\-])).{8,}$/)) {
  print('Okay!');
} else {
  print('Fail...');
}

打印:Okay!,正如您在 Ideone 上看到的那样.

prints: Okay!, as you can see on Ideone.

 
精彩推荐
图片推荐