Avatar of bishma
bishma

asked on 

MySQL regex for matching comma seperated values

I have a string matching question that is beyond my knowledge when it comes to regex in mysql.

The Situation:
I have a rather large table with csv fields that needs to be searched and I need to be able to match rows according to 1+ user specified search terms. For example the user needs to be able to search for clock and radio. The issue is that I only want it to match separate comma separated values. So searching for clock and radio would only match were the field contains "clock, whatever, radio" and not "clock radio"

My thinking:
I need a regular expression that will match;
- the beginning of the string, a comma, or a space
- search term 1
- zero or more spaces
- then either a comma or the end of the string
then an OR and the same thing for search term 2.

Thank you in advance
MySQL ServerRegular Expressions

Avatar of undefined
Last Comment
bishma
Avatar of ozo
ozo
Flag of United States of America image

(^|[,[:space:]])clock[[:space:]]*(,|$)|(^|[,[:space:]])radio[[:space:]]*(,|$)
Avatar of bishma
bishma

ASKER

Here's what I'm doing
SELECT * FROM `myTable` WHERE `bedroom` REGEXP (^|,|[:space:])clock[[:space:]]*(,|$)|(^|,|[:space:])radio[[:space:]]*(,|$)

I altered you expression slightly because, I believe what you sent you would have mathched comma AND space vs. comma or space. But obviously I could be wrong.

MySQL said:
ERROR: Unknown Punctuation String @ 56
STR: ^|

Where did I screw up?

BTW I'm using mysql 4.1
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ozo
ozo
Flag of United States of America image

,|[:space:] should be ,|[[:space:]] or [,[:space:]]
Avatar of bishma
bishma

ASKER

Worked perfectly. Thank you ozo, you're a gentleman/woman and a scholar.
MySQL Server
MySQL Server

MySQL is an open source, relational database management system that runs as a server providing multi-user access to a number of databases. Acquired by Oracle in 2009, it is frequently used in combination with PHP installations, powering most of the WordPress installations.

49K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo