I'd like to use MySQL's REGEXP to match multiple numbers of a csv in a MySQL query.
I am trying to identify if a CSV string contains numbers 2 and 9. The order matters in the result. They could be back to back, and be present at the beginning and/or end.
The below CSV strings should all produce positive result:
1,2,3,4,5,6,7,8,9,10
2,9,1,2,3,4,5,10
1,2,3,5,9
These CSV strings should not:
9,2,3,4,5,10 - (2 doesn't exist before 9)
2,1,2,3,4,5,10 - (9 not present)
I've tried to match what I am expecting in the pattern by the following logic:
- matching anything or nothing
- match the number 2 at least one time
- matching anything or nothing
- match the number 9 at least one time
- matching anything or nothing
My expression that is close, but not working is:
REGEXP '.*([^0-9][2][^0-9])+.*([^0-9][9][^0-9])+.*'
The above expression fails to match if 2 is the very beginning or 9 the very end of the string. Thanks for the input.