I'm trying to build a search query which searches for a word in a string and finds matches based on the following criteria:
- The word is surrounded by a combination of either a space, period or comma
- The word is at the start of the string and ends with a space, period or comma
- The word is at the end of the string and is followed by a space, period or comma
- It's a full match, i.e. the entire string is just the word
For example, if the word is 'php' the following strings would be matches:
- php
- mysql, php, javascript
- php.mysql
- javascript php
But for instance it wouldn't match:
- php5
I've tried the following query:
SELECT * FROM candidate WHERE skillset REGEXP '^|[., ]php[., ]|$'
However that doesn't work, it returns every record as a match which is wrong.
Without the ^| and |$ in there, i.e.
SELECT * FROM candidate WHERE skillset REGEXP '[., ]php[., ]'
It successfully finds matches where 'php' is somewhere in the string except the start and end of the string. So the problem must be with the ^| and |$ part of the regexp.
How can I add those conditions in to make it work as required?