简体   繁体   English

JAVA Regex解决密码验证问题

[英]JAVA Regex for password validation issue

i have a regexp for password validation ( Regexp Java for password validation with extra special characters) 我有一个用于密码验证的正则表达式( 用于带有特殊字符的密码验证的正则表达式Java

String pattern ="^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!\\*_?|~(){}/<>:\"\',\\[\\]`;\\\\\\\\-])(?=\\S+$).{8,}$";

The issue with this is, if i say 问题是,如果我说

 "Xyz.123".matches(pattern);

This returns false 这返回false

However, if i say 但是,如果我说

  "Xyz.123$".matches(pattern);

This returns true 这返回true

'.' '。' is not a valid special characters in my case. 在我的情况下不是有效的特殊字符。 But if my password has a valid special character along with '.' 但是,如果我的密码带有有效的特殊字符以及“。” it returns true 它返回true

You have \\\\S+ , which means "any non-space character." 您有\\\\S+ ,表示“任何非空格字符”。 . satisfies that. 满足。 You also need the $ to satisfy the third pseudo-condition (lookahead). 您还需要$来满足第三个伪条件(超前)。 . is not in that, but $ is. 不是在其中,而是$

EDIT: If you do not want any periods at all, change the last pseudo-condition to (?=[^\\\\s.]+$) 编辑:如果您根本不希望使用任何句点,请将最后一个伪条件更改为(?=[^\\\\s.]+$)

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

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