RegExp for a minimum of 4 numbers and a minimum of 1 character

I am trying to check if an input has at least 4 numbers and 1 character. It works for me, but only when the characters are in order 0000a, I wanted to fit regardless of the order, therefore 00a00, a0000, aa00a00awill match the pattern.

[0-9]{4,}[a-zA-Z]{1,}

What do i need to change? I tried [a-zA-Z0-9]{5,}, but then things like aaaaaand 00012are not suitable.

+4
source share
2 answers

Using lookahead statement:

(?=.*[a-zA-Z])(.*?\d){4,}

Regular expression visualization

Demo version of Debuggex

+8
source

If the formal rules are:

"Numbers >= 4 AND characters == 1"

, . - Pushdown .

"Numbers == 4 AND characters == 1" ( ):

([a-zA-Z][0-9]{4,})|([0-9][a-zA-Z][0-9]{3,})|([0-9]{2,}[a-zA-Z][0-9]{2,})|([0-9]{3,}[a-zA-Z][0-9])|([0-9]{4,}[a-zA-Z])

ruls "( + ) >= 4 >= 1" :

([a-zA-Z]([a-zA-Z]|[0-9]){4,})|(([a-zA-Z]|[0-9])[a-zA-Z]([a-zA-Z]|[0-9]){3,})|(([a-zA-Z]|[0-9]){2,}[a-zA-Z]([a-zA-Z]|[0-9]){2,})|(([a-zA-Z]|[0-9]){3,}[a-zA-Z]([a-zA-Z]|[0-9]))|(([a-zA-Z]|[0-9]){4,}[a-zA-Z])

- , . .

0

Source: https://habr.com/ru/post/1540231/


All Articles