以下是我验证密码的模式:
$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)
答案 0 :(得分:1)
你需要两次逃避正斜杠。您有时需要转义双斜杠的原因是它们被剥离两次 - 一次是PHP引擎(在编译时),一次是正则表达式引擎。
来自PHP manual:
单引号和双引号PHP字符串具有反斜杠的特殊含义。因此,如果\必须与正则表达式\匹配,则必须在PHP代码中使用“\\”或“\\”。
更新的正则表达式应如下所示:
$pattern = '/^[0-9A-Za-z!@#$^%*_|;:\'"`~.,\(\)\{\}\[\]\<\>\\\\\/\?\-\+\=\&]{6,}$/m';