簡體   English   中英

我的用於驗證密碼的正則表達式有什么問題?

[英]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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM