正则表达式接受4条规则中的3条 [英] Regex to accept 3 out of 4 rules

查看:95
本文介绍了正则表达式接受4条规则中的3条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我最初是这样写的:

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

因此,正如预期的那样,它成功匹配了5abcdefG,Abc @ defghi,5abcdefG〜等字符串.

问题在于它允许我提到的4个特殊字符以外的其他字符.因此,像1€abcdefG和Abc!defghi这样的字符串也可以匹配,但它们不应该匹配.我想念什么?

解决方案

要点是,您的.匹配除换行符之外的任何字符,因此它可以匹配除4个特殊字符,字母或数字之外的许多字符.

此外,将 OR 条件分成两个具有先行((?=(.*?[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@#&~]仅允许您在此字符类中指定的字母,数字和特殊字符.

此正则表达式也符合对比原则(超前失败或与否定的字符类匹配更快).

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?

解决方案

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).

这篇关于正则表达式接受4条规则中的3条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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