2

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!

1 Answer 1

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

Simply use this.See demo.

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

or

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

Just to be very safe.

Sign up to request clarification or add additional context in comments.

8 Comments

you are great this worked like a charm. Thanks!! one thing where do I learn this regex are there any video tuts or a good resource?
@Iglance3 regex101.com .... you can learn here......check explanation at right side....only practice can make you aficionado in regex.
Hey sorry to bother you, but It doesn't work with multiple keywords.. If there is a third keyword or more then it will not work...
ok I got my issue in my example I forgot the / /gim, thanks again
@Iglance3 that is not possible .Regex engine once matches moves out of alternation group.that will require you to build a specific regex for this.But it wont be generic.See regex101.com/r/vD5iH9/74
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.