0

How would I do the following in mysql?

SELECT * FROM table WHERE search REGEXP '.+season\d+\s?.+' limit 10;

I want to match something like:

"hello this is season1 how are you?"

But not:

"hello this is season1episode1 how are you?

2 Answers 2

2

You can use the following regular expression since \d and \s are not available on MySQL. You can use character classes instead.

You can replace \d with [[:digit:]] or [0-9] and \s with [[= =]] or [ ].

SELECT * FROM table WHERE search REGEXP '.+season[[:digit:]]+[[= =]].+' LIMIT 10

-- or...
SELECT * FROM table WHERE search REGEXP '.+season[0-9]+[ ].+' LIMIT 10

demo on dbfiddle.uk

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

4 Comments

Thanks -- what's the [= =] mean?
@David542 - [= =] matches space character
thanks for this great answer. I've never seen the [= =] before, just \s or ` . Where does [= =]` come from?
I think you are missing a pair of brackets.
0

Before MySQL 8.0,

REGEXP "season[0-9]+[[:>:]]"

meaning "season", at least one digit, then a word boundary. Note that it will stop with punctuation.

REGEXP "season[0-9]+[^a-zA-Z]"

Might work for you -- it says that it should be followed by a letter.

8.0 changes the word boundary to:

REGEXP "season[0-9]+\b"

(Caveat: the backslash may need to be doubled up.)

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.