domoindal
domoindal

Reputation: 1533

Mysql regex to find fixed lenght words

I'm trying to find words of three letters from a given set using the next syntax:

SELECT * FROM words WHERE word REGEXP '^[dcqaahii]{3}'

and it returns words with more than three letters. I thought that {3} limit the length of the output.

Any one know what's wrong in my expression?

Thanks in advance.

Upvotes: 2

Views: 3602

Answers (2)

ladenedge
ladenedge

Reputation: 13439

REGEXP, unlike LIKE, does not have to match the whole string. It only needs to match a portion of the string.

If you want columns that match your regex exactly, you need to include both the BOL and EOL characters in your regex:

'^[dcqaahii]{3}$'

regular-expressions.info has more details.

Upvotes: 1

Andrew Clark
Andrew Clark

Reputation: 208665

You need to add the end of string anchor $ to the end of your regex:

SELECT * FROM words WHERE word REGEXP '^[dcqaahii]{3}$'

Also, your can remove the extra a and i from your character class without changing the meaning: ^[dcqahi]{3}$

Upvotes: 1

Related Questions