正则表达式接受4条规则中的3条(Regex to accept 3 out of 4 rules)

编程入门 行业动态 更新时间:2024-10-11 09:25:40
正则表达式接受4条规则中的3条(Regex to accept 3 out of 4 rules)

我似乎无法使正则表达式符合以下要求:8到20长度的字符串,必须包含至少1个大写字母字符,至少1个小写字母字符,以及至少1个数字或至少1个特殊字符串角色(或两者)。 假设特殊字符仅限于包含@,#,&,〜。

我最初写了这个:

^(?=.*?[A-Z])(?=.*?[a-z])(?=(.*?[0-9])|(.*?[@#&~])).{8,20}$

正如预期的那样,它成功地匹配了像5abcdefG,Abc @ defghi,5abcdefG~等字符串。

问题是它允许的字符不是我提到的4个特殊字符。 所以像1€abcdefG和Abc!defghi这样的字符串也匹配,但它们不应该。 我错过了什么?

I can't seem to get the regex correct for the following requirement: a string between 8 and 20 length that must contain at least 1 uppercase alphabet character, at least 1 lowercase alphabet character, and either at least 1 digit or at least 1 special character (or both). Let's say special characters are restricted to include just @,#,&,~.

I wrote this initially:

^(?=.*?[A-Z])(?=.*?[a-z])(?=(.*?[0-9])|(.*?[@#&~])).{8,20}$

So as expected it successfully matches strings like 5abcdefG, Abc@defghi, 5abcdefG~, etc.

The problem is it allows characters OTHER than the 4 special ones I mentioned. So strings like 1€abcdefG and Abc!defghi also match, but they shouldn't. What am I missing?

最满意答案

关键是你的. 匹配任何字符但是换行符,因此它可以匹配除4个特殊字符,字母或数字之外的许多字符。

另外,将OR条件拆分为具有前瞻的2个备选分支是没有意义的( (?=(.*?[0-9])|(.*?[@#&~])) )。 您可以将该条件合并为一个(?=.*?[0-9@#&~]) 。 关键是正字符类中的范围/字符是“或”, [0-9@#&~]匹配数字,或@ ,或# ,或& ,或~ 。

我建议

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9@#&~]*[0-9@#&~])[A-Za-z0-9@#&~]{8,20}$

看到这个正则表达式演示

您还可以使用注释模式或块来构建动态模式,以使模式可读和可维护:

^ # start of string (?=[^A-Z]*[A-Z]) # string must have an uppercase letter (?=[^a-z]*[a-z]) # string must have a lowercase letter (?=[^0-9@#&~]*[0-9@#&~]) # string must have a digit or defined special char [A-Za-z0-9@#&~]{8,20} # The string should have 8 to 20 symbols from the defined set $ # end of string

[A-Za-z0-9@#&~]只允许您在此字符类中指定的字母,数字和特殊字符。

这个正则表达式也符合对比原则(前瞻失败或与否定字符类更快地匹配)。

The point is that your . matches any char but a newline, so it can match a lot of characters other than your 4 special chars, letters or digits.

Also, it makes no sense to split OR condition into 2 alternative branches with lookaheads ((?=(.*?[0-9])|(.*?[@#&~]))). You can merge that condition into a single (?=.*?[0-9@#&~]). The point is that the ranges/chars inside the positive character class are "OR'ed", [0-9@#&~] matches either a digit, or @, or #, or &, or ~.

I suggest

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9@#&~]*[0-9@#&~])[A-Za-z0-9@#&~]{8,20}$

See this regex demo

You may also use comment mode or blocks to build a dynamic pattern to make the pattern readable and maintainable:

^ # start of string (?=[^A-Z]*[A-Z]) # string must have an uppercase letter (?=[^a-z]*[a-z]) # string must have a lowercase letter (?=[^0-9@#&~]*[0-9@#&~]) # string must have a digit or defined special char [A-Za-z0-9@#&~]{8,20} # The string should have 8 to 20 symbols from the defined set $ # end of string

The [A-Za-z0-9@#&~] will only allow letters, digits, and special chars you specify in this character class.

This regex also conforms to the principle of contrast (lookaheads fail or match quicker with negated character classes).

更多推荐

本文发布于:2023-08-04 17:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:规则   正则表达式   Regex   accept   rules

发布评论

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

>www.elefans.com

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