Reputation: 117
I need a Regex to accept the input only following possible cases like
1.It start with characters only [a-zA-Z]
2.It may contains numbers but it may not repeated 3 or moretimes.
Example:
akjsjdfljsfjl133113 Valid
123123sfsf not valid
asfsdf1111asdf notvalid adf111 not valid
I have tried this code
$input_line="sfjs123232";
preg_match("/^[a-zA-Z](\d)\1{2,}/", $input_line, $output_array);
Upvotes: 3
Views: 757
Reputation: 70722
You can use a Negative Lookahead here.
^[a-zA-Z]+(?:(?!.*(\d)\1{2,}).)*$
See Live demo
Regular expression
^ 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
Upvotes: 4