tags:

views:

106

answers:

3

Hi

I have table with rows of strings. I'd like to search for those strings that consists of only two words.

I tried few ways with [[:space:]] etc but mysql was returning three, four word strings also

+1  A: 

^\w+\s\w+$ should do well.

Note; what I experience more often in the last days is that close to nobody uses the ^$-operators.

They are absolutely needed if you want to tell if a string starts or ends with something or want to match the string exactly, word for word, as you. "Normal" strings, like you used (I assume you used something like \w[:space]\w match in the string, what means that they also match if the condition is true anywhere within the string!

Keep that in mind and Regex will serve you well :)

ApoY2k
That was happening. As to "^\w\s\w+$" it didn't show results. I got mysql 5.0, maybe thats the reason.
Chris
+1  A: 

try this:

select * from yourTable WHERE field REGEXP('^[[:alnum:]]+[[:blank:]]+[[:alnum:]]+$');

more details in link : http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Haim Evgi
A: 
 REGEXP ('^[a-z0-9]*[[:space:]][a-z0-9]*$')
Chris