0

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

^\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 :)

1 Comment

That was happening. As to "^\w\s\w+$" it didn't show results. I got mysql 5.0, maybe thats the reason.
0
 REGEXP ('^[a-z0-9]*[[:space:]][a-z0-9]*$')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.