[英]Regex for Input Validation in PHP
我需要一個正則表達式,只接受輸入后的可能情況
1.它僅以字符開頭[a-zA-Z]
2.它可能包含數字,但可能不會重復3次或更多次。
示例 :
akjsjdfljsfjl133113有效
123123sfsf無效
asfsdf1111asdf無效adf111無效
我試過這段代碼
$input_line="sfjs123232";
preg_match("/^[a-zA-Z](\d)\1{2,}/", $input_line, $output_array);
您可以在此處使用否定前瞻 。
^[a-zA-Z]+(?:(?!.*(\d)\1{2,}).)*$
查看現場演示
正則表達式
^ the beginning of the string
[a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z' (1 or more times)
(?: group, but do not capture (0 or more times)
(?! look ahead to see if there is not:
.* any character except \n (0 or more times)
( group and capture to \1:
\d digits (0-9)
) end of \1
\1{2,} what was matched by capture \1 (at least 2 times)
) end of look-ahead
. any character except \n
)* end of grouping
$ before an optional \n, and the end of the string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.