1

I am trying to write a MySQL statement which finds and returns the book registrations that contain 2 or more spaces in a row.

The statement below is wrong.

SELECT * FROM book WHERE titles REGEXP '[:space]{2,}';
7
  • Can you explain why this statement does not work for you? Commented Sep 17, 2015 at 11:03
  • Try with SELECT * FROM book WHERE titles LIKE '%<two_spaces>%'. Since the 2 spaces already meet your condition, you really do not need to check if there are more than 2. I have just tried that and it seems to work. If you need to match a regular ASCII space (dec. code 32), you do not need a REGEXP. Commented Sep 17, 2015 at 11:05
  • because I'm getting also results which have just 1 space Commented Sep 17, 2015 at 11:06
  • @sotirios: Don't you think LIKE is more efficient? Unless you need to match any whitespace, I'd choose a LIKE-based solution. Commented Sep 17, 2015 at 11:14
  • I haven't understood exactly the difference between like and regexp yet. I am beginner. I think your like statement can return results with exactly 2 spaces. Not more. Commented Sep 17, 2015 at 11:27

3 Answers 3

2

Since the 2 spaces already meet your condition, you really do not need to check if there are more than 2. Moreover, if you need to match a regular ASCII space (decimal code 32), you do not need a REGEXP operator, you can safely use

SELECT * FROM book WHERE titles LIKE '%  %';

LIKE is preferred in all cases where you can use it instead of REGEXP (see MySQL | REGEXP VS Like)

When you need to match numerous whitespace symbols, you can use WHERE titles REGEXP '[[:space:]]{2}' (it will match [ \t\r\n\v\f]), and if you only plan to match tabs and spaces, use WHERE titles REGEXP '[[:blank:]]{2}'. For more details, see POSIX Bracket Expressions.

Note that [:class_name:] should only be used inside a character class (i.e. inside another pair of [...], otherwise, they are not recognized.

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

Comments

1

Your POSIX class must be,

SELECT * FROM book WHERE titles REGEXP '[[:space:]]{2,}';

No need for ,

SELECT * FROM book WHERE titles REGEXP '[[:space:]]{2}';

You may also use [[:blank:]]

SELECT * FROM book WHERE titles REGEXP '[[:blank:]]{2}';

4 Comments

sorry, I must find 2 spaces or more. I am going to edit the question. sorry again
ya, atleast two spaces.. My regex should satisfy the rules.
This finds exactly two: '[[:space:]]{2}'
@Musa atleast two or more. So exactly 2 comes within atleast two.
0

If you mean just the space character: REGEXP ' '. Or you could use LIKE "% %", which would be faster. (Note: there are 2 blanks in those.)

Otherwise, see http://dev.mysql.com/doc/refman/5.6/en/regexp.html for blank and space.

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.