Shahin Shadfar
Shahin Shadfar

Reputation: 13

Regex for specific pattern - String followed by numbers

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".

Upvotes: 1

Views: 650

Answers (3)

Mohammad Javad Noori
Mohammad Javad Noori

Reputation: 1217

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]+)?[[:>:]]'

Upvotes: 2

Shahin Shadfar
Shahin Shadfar

Reputation: 13

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.

Upvotes: 0

vicky
vicky

Reputation: 189

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

Upvotes: 1

Related Questions