我需要知道以下情况的正则表达式:
至少8个字符( ... ).{8,}
字母为(?=.*[a-z|A-Z])
编号为(?=.*\d)
有特殊字符(?=.*[~'!@#$%?\\\/&*\]|\[=()}"{+_:;,.><'-])
我在其他正则表达式中得到以下内容:
((?=.*\d)(?=.*[a-z|A-Z])(?=.*[~'!@#$%?\\\/&*\]|\[=()}"{+_:;,.><'-])).{8,}
但是它失败了:
qwer!234
有小费吗?
最佳答案
使用所有这些特殊字符,很可能您没有适当地转义所有内容。
你说Java对吗?打印true
:
String regex = "((?=.*\\d)(?=.*[a-zA-Z])(?=.*[~'!@#$%?\\\\/&*\\]|\\[=()}\"{+_:;,.><'-])).{8,}";
System.out.println("qwer!234".matches(regex));
但这要简单得多:
String regex = "(?=.*?\\d)(?=.*?[a-zA-Z])(?=.*?[^\\w]).{8,}";