我需要一个正则表达式才能接受输入,只有在可能的情况下接受
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);
答案 0 :(得分:4)
您可以在此处使用否定前瞻。
^[a-zA-Z]+(?:(?!.*(\d)\1{2,}).)*$
请参阅Live demo
正则表达式
^ 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