David Mirwis
David Mirwis

Reputation: 21

REGEXP and operator .+

I am trying to use MySql REGEXP to find rows where green and 2012 occurs in the column

I am using .+ in the regexp.

This works:

select  'green 2012-01' REGEXP 'green.+2012'

returns 1

but if I place the '2012' first it returns 0

select  'green 2012-01' REGEXP '2012.+green';

returns 0

I am using MYSQL Software version: 5.1.43 - MySQL Community Server (GPL)

Upvotes: 2

Views: 117

Answers (2)

raykendo
raykendo

Reputation: 640

Regular expressions are kinda order dependent. What you'll need to do is put an | (or) operator between your two items to make it look for either one.

select 'green 2012-01' REGEXP '(green.*2012)|(2012.*green)'

Upvotes: 2

Jason McCreary
Jason McCreary

Reputation: 73031

As an alternative to REGEX, while potentially less efficient, you could simply use LOCATE twice.

SELECT * FROM table WHERE LOCATE('2012', column) AND LOCATE('green', column);

Upvotes: 0

Related Questions