Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp for minimum 4 numbers and minimum 1 character

Tags:

regex

I'm trying to test if an input has at least 4 number and 1 character in it. I have this which works, but only when the characters are in the order 0000a, I wanted it to match no matter what the order, so 00a00, a0000, aa00a00a would all 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 aaaaa and 00012 matched, which is no good.

like image 304
TMH Avatar asked Feb 14 '23 00:02

TMH


1 Answers

Using lookahead assertion:

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

Regular expression visualization

Debuggex Demo

like image 66
falsetru Avatar answered Feb 19 '23 21:02

falsetru