mahen3d mahen3d - 1 month ago 8
MySQL Question

MySQL regular expression to Count How many number of digits are in the username (String)?

I need a MySQL regular expression to count the number of digits in the user's user name.

i.e. if it has more than 7 numbers needs to be selected.

I tried following expression but it gives me error

SELECT * FROM `user` WHERE `name` REGEXP '^\D*(?:\d\D*){7,}$'

Error i get is

#1139 - Got error 'repetition-operator operand invalid' from regexp


You're using \d and \D and (?:) syntax but MySQL doesn't support those extended regular expression metacharacters.

If it has more than 7 numbers it need to be selected.

So I would guess you can assume the username is valid, i.e. you don't need to check that it starts with a non-digit. And you might accept a username that has more than 7 digits as well as at least 7 digits. I'm also assuming the digits don't need to be together, there just have to be 7 of them.

Here's a regexp that works:

SELECT * FROM `user` WHERE `name` REGEXP '([[:digit:]].*){7}';