mysqlregexstringword-boundary

REGEXP match either keywords in the string using MySQL


I am trying to search for a keyword in the string using REGEXP, how I want to search is:

Stored String 1: "The quick brown fox jumps over the lazy dog" Stored String 2: "The clever rabbit ran away from the tiger" Keywords: "Clever| Fox" (The two keywords separated by the delimiter "|")

I want to search for a stories which contains the either one of the keywords or all of the keywords.

I used str_replace the keyword string to make a keyword string a regular expression that can be used in mysql query. I tried many solutions which are provided here to previously asked questions but I found nothing working for me.

One of what I have tried so far is this:

SELECT * FROM stories WHERE story_body REGEXP '[[:<:]]Clever[[:>:]].*[[:<:]] Fox[[:>:]]'

How do I achieve this? Thanks!


Solution

  • ^.*(Clever|Fox).*$
    

    Simply use this.See demo.

    https://regex101.com/r/vD5iH9/55

    or

    ^.*\b(Clever|Fox)\b.*$
    

    Just to be very safe.