简体   繁体   English

正则表达式不匹配

[英]Regular Expression not matching

I'm trying to create a RegExp to match new passwords (which works), but I only need a last step to make it work 100%. 我正在尝试创建一个RegExp以匹配新密码(这是有效的),但我只需要最后一步使其100%工作。

Here's the RegExp and what it is supossed to: 这是RegExp以及它的用途:

((?=.*(\\d|[\\(\\)\\{\\}\\?!\\$&\\*%\\=\\+_\\-\\.]))(?=.*[a-z])(?=.*[A-Z]).{8,})

The RegExp says that: Digits OR Symbols (){}?!$&%*=+-. RegExp说:数字符号(){}?!$&%*=+-. must be used -and that's what doesn't work, the OR operator, as I can insert both numbers and symbols - , at least one lowercase, at least one uppercase and a minimum lenght of 8 characters. 必须使用- 这是不起作用的,OR运算符,因为我可以插入数字和符号 - ,至少一个小写,至少一个大写和最小长度为8个字符。

I've tried to use the OR operator | 我试过使用OR运算符| in several ways, but I can't make it work. 在几个方面,但我无法使其工作。

What am I missing? 我错过了什么? Thank you very much in advance. 非常感谢你提前。

Note: I'm using this regular expression within a liferay configuration file for the password policies. 注意:我在liferay配置文件中使用此正则表达式来表示密码策略。

You want a XOR logical operation, not a OR. 您想要一个XOR逻辑运算,而不是OR。

OR is true:
A |  B |  o/p
T |  T |   T
F |  T |   T
T |  F |   T
F |  F |   F

XOR is true:
A |  B | o/p
T |  T |  F
F |  T |  T
T |  F |  T
F |  F |  F

Exclusive Or in Regular Expression 独家或正则表达

Ok, I've rewritten your expression slightly, this is what I came up with: 好的,我稍微改写了你的表达,这就是我提出的:

String pattern = "^(?=.*[\\d().{}?!$&*%=+_-])(?=.*[a-z])(?=.*[A-Z]).{8,}$";

This matches any string with 这匹配任何字符串

  • at least one number or special character 至少一个数字特殊字符
  • at least one lower-case letter 至少一封小写字母
  • at least one upper-case letter 至少有一个大写字母
  • is at least 8 characters long 至少8个字符

I have to say that, while regex can probably do this, I think it's simpler and safer to use multiple regex tests, something like: 我不得不说,虽然正则表达式可能会这样做,但我认为使用多个正则表达式测试更简单,更安全,例如:

boolean containsDigit = Pattern.matches("[0-9]", testString);
boolean containsSymbol = Pattern.matches("[(){}?!$&%*=+.-]", testString);
boolean containsLowercase = Pattern.matches("[a-z]", testString);
boolean containsUppercase = Pattern.matches("[A-Z]", testString);

if(testString.length() >= 8
        && containsLowercase
        && containsUppercase
        && (containsDigit || containsSymbol)
        && !(containsDigit && containsSymbol)) {
    //valid
} else {
    //invalid
}

I was using a Java regex pattern a few months ago that was not wildly complex, but it led to a Java bug being revealed. 几个月前我使用的是一个Java正则表达式模式并不是非常复杂,但它导致了一个Java漏洞被揭示出来。 Too many parenthetic clauses seemingly caused trouble, and I had to split the pattern into something simpler. 太多令人满意的条款似乎造成了麻烦,我不得不将模式分解成更简单的东西。

The above is much more readable than a multi-clause regex pattern, and therefore easier to maintain and debug. 上面的内容比多子句正则表达式模式更易读,因此更易于维护和调试。

You said, 你说,

I can insert both numbers and symbols 我可以插入数字和符号

That would be normal with a logical OR operation. 对于逻辑OR运算,这是正常的。 It sounds like what you want is exclusive or (XOR). 听起来你想要的是独家或(XOR)。 I'm afraid I don't know how you do that in regex, but hopefully this will point you in the right direction. 我担心我不知道你是如何在正则表达式中那样做的,但希望这会指出你正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM