期望如何匹配至少一个数字、一个小写、一个大写的模式

时间:2021-12-10 17:27:43

using Expect how can I check whether the given input has at least one upper case,at least one lowercase and at least one number or "!-%#"? For a similar question where the min number of input string is 10, someone answered

使用Expect,我如何检查给定输入是否有至少一个大小写和至少一个数字或“!-%#”?对于输入字符串的最小数目为10的类似问题,有人回答

expect  ^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])([!-%#])*.{10,}$

As I'm new to Tcl and Expect, can anyone please explain how the above code works? Why is it not working if we remove {10,} in order to remove the minimum limit?

由于我是Tcl的新手,希望有人能解释一下上面的代码是如何工作的?如果我们删除{10,}以删除最小限制,为什么它不工作?

1 个解决方案

#1


2  

The min limit cannot be removed by deleting the limiting (bound) quantifier {10,}, you need to replace it with * (to allow empty string) or + (to require at least 1):

通过删除限制(绑定)量词{10,}不能删除最小限制,您需要用*(允许空字符串)或+(至少需要1)替换它:

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-]).*$

or just (since expect does not require a full string match):

或者仅仅(因为expect不需要完整的字符串匹配):

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-])

If you need to restrict the symbols to those inside the lookaheads, just replace .* with [a-zA-Z0-9#?!@$%^&*-]*:

如果你需要限制符号在超前,只是取代。*与[a-zA-Z0-9 # ? ! @ $ % ^ & *()*:

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-])[a-zA-Z0-9#?!@$%^&*-]*$ 
                                                    ^^^^^^^^^^^^^^^^^^^^^

I'd also use greedy quantifiers in a consistent manner in a Tcl regex, although that makes no difference in the current pattern.

我还将在Tcl regex中以一致的方式使用贪婪量词,尽管这在当前模式中没有区别。

Details:

细节:

  • ^ - start of string
  • ^ -字符串的开始
  • (?=.*[A-Z]) - after any 0+ chars there must be an uppercase ASCII letter
  • (?=.*[A-Z]) -在任何0+字符之后,必须有一个大写的ASCII字符
  • (?=.*[a-z]) - after any 0+ chars there must be an lowercase ASCII letter
  • (?=.*[a-z]) -在任何0+字符之后,必须有一个小写的ASCII字母
  • (?=.*[0-9#?!@$%^&*-]) - after any 0+ chars there must be an ASCII digit, or any of the special symbols defined in the character class
  • (? =。*[0 - 9 # ? ! @ $ % ^ & *())——在任何0 +字符必须有一个ASCII数字,或任何特殊字符类中定义的符号
  • .* - any 0+ characters, as many as possible up to
  • .* -任何0+字符,最多
  • $ - the end of string.
  • $ -字符串的末尾。

The (?=...) constructs are called positive lookaheads that "stand their ground", i.e. do not advance the regex index once they are executed. Thus, the 3 lookaheads will perform the check one by one, and once one of them returns false, the whole match will be failed.

(?=…)构造被称为“站在他们的立场上”的积极的lookahead,也就是说,一旦它们被执行,就不要推进regex索引。因此,3个lookaheads将逐个执行检查,一旦其中一个返回false,整个匹配将失败。

Note: to avoid unnecessary backtracking with .*, you may apply the principle of contrast (i.e. using corresponding negated character classes inside the lookaheads):

注意:为了避免不必要的回溯,*,你可以应用对比原则(即使用相应的否定字符类在lookaheads内):

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!@$%^&*-]*[0-9#?!‌​@$%^&*-])[a-zA-Z0-9#‌​?!@$%^&*-]*$

The [^A-Z]* will match one or more characters other than uppercase ASCII letters, [^a-z]*will match one or more characters other than lowercase ASCII letters, etc. The .* grabs the whole string first, and then backtracks, checking if the subsequent subpattern can match. So, negated character classes are much more efficient and find or fail the match quicker.

^[a - z]*将匹配一个或多个字符以外的ASCII字母,大写^[a - z]*将匹配一个或多个字符小写ASCII字母以外,等。*抓住整个字符串的第一个,然后放弃,检查如果后续子模式匹配。因此,否定字符类的效率要高得多,可以更快地找到或失败匹配。

#1


2  

The min limit cannot be removed by deleting the limiting (bound) quantifier {10,}, you need to replace it with * (to allow empty string) or + (to require at least 1):

通过删除限制(绑定)量词{10,}不能删除最小限制,您需要用*(允许空字符串)或+(至少需要1)替换它:

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-]).*$

or just (since expect does not require a full string match):

或者仅仅(因为expect不需要完整的字符串匹配):

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-])

If you need to restrict the symbols to those inside the lookaheads, just replace .* with [a-zA-Z0-9#?!@$%^&*-]*:

如果你需要限制符号在超前,只是取代。*与[a-zA-Z0-9 # ? ! @ $ % ^ & *()*:

expect  ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9#?!@$%^&*-])[a-zA-Z0-9#?!@$%^&*-]*$ 
                                                    ^^^^^^^^^^^^^^^^^^^^^

I'd also use greedy quantifiers in a consistent manner in a Tcl regex, although that makes no difference in the current pattern.

我还将在Tcl regex中以一致的方式使用贪婪量词,尽管这在当前模式中没有区别。

Details:

细节:

  • ^ - start of string
  • ^ -字符串的开始
  • (?=.*[A-Z]) - after any 0+ chars there must be an uppercase ASCII letter
  • (?=.*[A-Z]) -在任何0+字符之后,必须有一个大写的ASCII字符
  • (?=.*[a-z]) - after any 0+ chars there must be an lowercase ASCII letter
  • (?=.*[a-z]) -在任何0+字符之后,必须有一个小写的ASCII字母
  • (?=.*[0-9#?!@$%^&*-]) - after any 0+ chars there must be an ASCII digit, or any of the special symbols defined in the character class
  • (? =。*[0 - 9 # ? ! @ $ % ^ & *())——在任何0 +字符必须有一个ASCII数字,或任何特殊字符类中定义的符号
  • .* - any 0+ characters, as many as possible up to
  • .* -任何0+字符,最多
  • $ - the end of string.
  • $ -字符串的末尾。

The (?=...) constructs are called positive lookaheads that "stand their ground", i.e. do not advance the regex index once they are executed. Thus, the 3 lookaheads will perform the check one by one, and once one of them returns false, the whole match will be failed.

(?=…)构造被称为“站在他们的立场上”的积极的lookahead,也就是说,一旦它们被执行,就不要推进regex索引。因此,3个lookaheads将逐个执行检查,一旦其中一个返回false,整个匹配将失败。

Note: to avoid unnecessary backtracking with .*, you may apply the principle of contrast (i.e. using corresponding negated character classes inside the lookaheads):

注意:为了避免不必要的回溯,*,你可以应用对比原则(即使用相应的否定字符类在lookaheads内):

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!@$%^&*-]*[0-9#?!‌​@$%^&*-])[a-zA-Z0-9#‌​?!@$%^&*-]*$

The [^A-Z]* will match one or more characters other than uppercase ASCII letters, [^a-z]*will match one or more characters other than lowercase ASCII letters, etc. The .* grabs the whole string first, and then backtracks, checking if the subsequent subpattern can match. So, negated character classes are much more efficient and find or fail the match quicker.

^[a - z]*将匹配一个或多个字符以外的ASCII字母,大写^[a - z]*将匹配一个或多个字符小写ASCII字母以外,等。*抓住整个字符串的第一个,然后放弃,检查如果后续子模式匹配。因此,否定字符类的效率要高得多,可以更快地找到或失败匹配。