Combine REGEX expressions for MySQL query

Bob Stout :

I have a query that looks for improperly entered data in a field. The field should contain a string containing between 5 to 8 digits.

The query I have now is:

"...WHERE NOT(sid REGEXP '[0-9]{5}') AND NOT(sid REGEXP '[0-9]{6}') 
AND NOT(sid REGEXP '[0-9]{7}') AND NOT (sid REGEXP '[0-9]{8}')".

Is there a more elegant/shorter way to do this?

beltouche :
WHERE NOT(sid REGEXP '[0-9]{5,8}')

NB: As Bernd Buffen noted, this will still allow anything over 8 digits because, e.g., a 9-digit string contains an 8-digit string. You will have to add some kind of information defining the string boundaries. For example, if the string only consists of the digits, you want

WHERE NOT(sid REGEXP '^[0-9]{5,8}$')

If it's a "word" inside a larger string, you could use something like

WHERE NOT(sid REGEXP '\W[0-9]{5,8}\W')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13786&siteId=1
Recommended