1

We're trying to use a REGEX expression inside MySQL. Say we have a 2-column table with 5 rows as follow:

1  marketing
2  marketing1
3  marketing12
4  office5
5  marketing44Tomorrow

I'd like to have a SELECT statement that returns: marketing, marketing1, marketing12. Meaning a string (marketing) followed by nothing or by a number only.

This statement:

select * from ddd
where column_name2 REGEXP 'marketing[0-9]'

doesn't work as it does not return "marketing" alone and it will return "marketing44Tomorrow".

2
  • Try it with MySQL regex word boundery [[:<:]] [[:>:]] Commented Feb 15, 2018 at 20:57
  • Can you elaborate in my case? Commented Feb 15, 2018 at 21:18

3 Answers 3

2

You can use : marketing([0-9]+)?[[:>:]]

`marketing` - any word start with **marketing**
`([0-9]+)` - any digit where....

 1. `?` - Maybe there may there not 
 2. `[[:>:]]` - Must be the last

Result:

SELECT * FROM ddd WHERE column_name2 REGEXP 'marketing([0-9]+)?[[:>:]]'
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. When I enter this exact expression in my SELECT, it returns nothing. The problem is in the "\b". Without it, it works but then it also returns the "marketing44Tomorrow". Can you tell me how to use this in MySQL?
you can use [[:>:]] Instead of \b
select * from ddd where column_name2 REGEXP 'marketing([0-9]+)?[[:>:]]' works. Looks like the equivalent of "\b" in MySQL is [[:>]].
1

try this, select * from ddd where column_name2 REGEXP 'marketing[0-9]$'

1 Comment

This does not work as it does not return "marketing" alone and other issues.
0

As a conclusion, the perfect answer to my question in the MySQL context is:

SELECT * FROM ddd WHERE column_name2 REGEXP 'marketing([0-9]+)?[[:>:]]'

"MJN Belief" got it almost right up here.

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.