Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for 3 digits (never-mind the order)?

Is there any regex (!) which can test if a string contains 3 digits ( never-mind the order) ?

(at least 3 digits. also I be happy to see the exact 3 solution.( if you're kind ))

example :

abk2d5k6 //3 digits
abk25k6d //same here //3 digits

my fail tries :

.+?(?=\d){3}

Thanks.

(only regex solutions please , for learning purpose.).

like image 460
Royi Namir Avatar asked Nov 25 '12 09:11

Royi Namir


1 Answers

Well, for the learning purpose, I suggest to read through this very comprehensive tutorial.

Otherwise, the JavaScript regex would probably look something like this:

if you want to make sure that there are at least three digits:

/^(?:\D*\d){3}/

Or if you want to make sure that there are exactly three digits:

/^(?:\D*\d){3}\D*$/

\D is any non-digit character. * allows 0 or more of those. \d is any digit character. {3} repeats the subpattern 3 times. And ^ and $ mark the start and end of the string, respectively.

like image 176
Martin Ender Avatar answered Sep 29 '22 21:09

Martin Ender