regex - 至少一个大写、一个小写、一个数字和一个特殊字符的正则表达式
问题描述
我正在编写一个 android 正则表达式来检查包含的密码,我使用的正则表达式是
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{8,16}$
但我的 Matcher 类总是返回 false
Matcher matcher;
Pattern pattern = Pattern.compile(context.getString(R.string.password_validation_value));
matcher = pattern.matcher(password); // always returns false
我该如何解决这个问题?
解决方案
好的,这里有一个解释
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\p{P}).{6,16}$
解释
^ # Beginning of string
(?= .* \d ) # Assert, string contains a digit
(?= .* [a-z] ) # Assert, string contains a lower case letter
(?= .* [A-Z] ) # Assert, string contains a upper case letter
(?= .* \p{P} ) # Assert, string contains a punctuation character
.{6,16} # Consume 6 to 16 characters
$ # End of string
推荐阅读
- python - 如何从 docker 容器中的 python 脚本连接到 localhost 上的 mysql 数据库
- python - Python中时间序列的牛顿算法函数
- python-3.x - Python:一个线程如何在恢复执行之前等待其他线程结束?
- ios - 翠鸟:'setBackgroundImage(with:for:placeholder:options:progressBlock:completionHandler:)' 的模糊使用
- javascript - 调用时如何将未知的附加参数传递给回调函数?
- html - 显示:内联块在手机上不起作用
- c# - 操作员 '!' 不能应用于 x 类型的操作数
- javascript - 如何将带有“nd”、“rd”、“th”的日期转换为 yyyy-mm-dd 格式 Javasciript
- react-native - React-navigation 如何在 redux 中导航到路由
- php - 如何使用 php gettext 在应用程序中获取翻译语言列表