Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 至少用于大写、小写、数字和特殊字符的正则表达式_Regex_Regex Lookarounds_Regex Group_Regex Greedy - Fatal编程技术网

Regex 至少用于大写、小写、数字和特殊字符的正则表达式

Regex 至少用于大写、小写、数字和特殊字符的正则表达式,regex,regex-lookarounds,regex-group,regex-greedy,Regex,Regex Lookarounds,Regex Group,Regex Greedy,我正在写一个android regular express来检查密码,其中包含我使用的正则表达式 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{8,16}$ 但是我的Matcher类总是返回一个false Matcher matcher; Pattern pattern = Pattern.compile(context.getString(R.string.password_validation_value)); mat

我正在写一个android regular express来检查密码,其中包含我使用的正则表达式

^(?=.*\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

只需在每个断言的点后加一个量词。示例
(?=.*\d)
。另外,不要使用
\W
,因为这会匹配空格和控制字符。或者,用这个来代替
(?=.*(=[\x21-\x7e])[\W\uz])
@Aaron我不知道understand@sln请我也不明白你的答案,你能进一步解释一下吗?另外,我是这个regex的新手,因为你编辑了你的问题,我的评论是无效的(我已经删除了它),但是当你有
^(?=。。。)在字符串的开头,检查下一个字符是否为任意字符,以下字符是否为数字。还要检查下一个字符是否为whatever,后面是否为小写字母”。当然,第二个字符不能同时为两个字符。现在您使用了
(?=.*thing)
这不再是一个问题,因为这将匹配字符串中的任何位置,而不是第二个字符
matcher=pattern.matcher(password);
不会返回
false
。之后是否运行
matcher.matches()