什么';我用于验证密码的正则表达式有问题


What's wrong with my regex for validating password?

以下是我验证密码的模式:

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

我使用函数preg_match来验证

preg_match($pattern, $string);

但当我运行它时,它会显示以下错误:Warning: preg_match(): Unknown modifier ''' in xxx on line 13

我的正则表达式出了什么问题?

下面是解释的正则表达式:http://regex101.com/r/rR6uH0/

^ assert position at start of a line
[0-9A-Za-z!@#$^%*_|;:'"`~.,'(')'{'}'[']'<'>'''/'?'-'+'='&]{6,} match a single character present in the list below
Quantifier: Between 6 and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
!@#$^%*_|;:'"`~., a single character in the list !@#$^%*_|;:'"`~., literally
'( matches the character ( literally
') matches the character ) literally
'{ matches the character { literally
'} matches the character } literally
'[ matches the character [ literally
'] matches the character ] literally
'< matches the character < literally
'> matches the character > literally
'' matches the character ' literally
'/ matches the character / literally
'? matches the character ? literally
'- matches the character - literally
'+ matches the character + literally
'= matches the character = literally
'& matches the character & literally
$ assert position at end of a line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

您需要跳过两次正向斜杠。有时需要转义双斜杠的原因是它们被剥离了两次——一次是由PHP引擎(在编译时)剥离,另一次是正则表达式引擎剥离。

来自PHP手册:

单引号和双引号PHP字符串具有反斜杠的特殊含义。因此,如果''必须与正则表达式匹配,那么在PHP代码中必须使用"''''"或"''''"。

更新后的正则表达式应该如下所示:

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