Here is my pattern for validating password:
这是我验证密码的模式:
$pattern = '/^[0-9A-Za-z!@#$^%*_|;:\'"`~.,\(\)\{\}\[\]\<\>\\\/\?\-\+\=\&]{6,}$/m';
I use function preg_match
to validate
我使用函数preg_match来验证
preg_match($pattern, $string);
But when I run it, it shows this error: Warning: preg_match(): Unknown modifier '\' in xxx on line 13
但是当我运行它时,它会显示以下错误:警告:preg_match():第13行的xxx中的未知修饰符'\'
What's wrong with my regex?
我的正则表达式有什么问题?
Here is the regular expression explained: http://regex101.com/r/rR6uH0/
这是正则表达式解释: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)
1 个解决方案
#1
1
You need to escape the forward slashes twice. The reason why you sometimes need to escape double slashes is that they are stripped twice -- once by PHP engine (at compile time) and once by regular expression engine.
你需要两次逃脱正斜杠。你有时需要转义双斜杠的原因是它们被剥离了两次 - 一次是PHP引擎(在编译时),一次是正则表达式引擎。
From the PHP manual:
从PHP手册:
Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \, then "\\" or '\\' must be used in PHP code.
单引号和双引号PHP字符串具有反斜杠的特殊含义。因此,如果\必须与正则表达式\匹配,则必须在PHP代码中使用“\\”或“\\”。
The updated regular expression should look like:
更新的正则表达式应如下所示:
$pattern = '/^[0-9A-Za-z!@#$^%*_|;:\'"`~.,\(\)\{\}\[\]\<\>\\\\\/\?\-\+\=\&]{6,}$/m';
#1
1
You need to escape the forward slashes twice. The reason why you sometimes need to escape double slashes is that they are stripped twice -- once by PHP engine (at compile time) and once by regular expression engine.
你需要两次逃脱正斜杠。你有时需要转义双斜杠的原因是它们被剥离了两次 - 一次是PHP引擎(在编译时),一次是正则表达式引擎。
From the PHP manual:
从PHP手册:
Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \, then "\\" or '\\' must be used in PHP code.
单引号和双引号PHP字符串具有反斜杠的特殊含义。因此,如果\必须与正则表达式\匹配,则必须在PHP代码中使用“\\”或“\\”。
The updated regular expression should look like:
更新的正则表达式应如下所示:
$pattern = '/^[0-9A-Za-z!@#$^%*_|;:\'"`~.,\(\)\{\}\[\]\<\>\\\\\/\?\-\+\=\&]{6,}$/m';