1

I have a specified problem.

I have, for example: Hello little, hello red, sometime and now.

I need regexp what will be match only if the clause contains both hello and red.

I have complete a regexp ((hello|red).*){2}()* what will work only if in clause one word hello and one word red, but in my clause two words hello and regexp will work without word red, what wrong:(

I need regexp what will match only if cause contain word hello and word red and not depend of repeat one of them.

Please help.

Thx.

1
  • Your regular expression works because hello is indeed found twice, you did not require red specifically to be there, but either hello or red. Commented May 5, 2014 at 6:07

3 Answers 3

4

Usually, one would use positive lookahead assertions for this task, but MySQL's regex engine doesn't support them.

Therefore, your only option (if you want to do this in a single regex) is to handle both variations (hello after red or hello before red) "manually":

hello.*red|red.*hello

For two "search words", that's probably acceptable - it doesn't scale nicely, though.

Your regex ((hello|red).*){2}()* is a bit strange; it means

(            # Start of group:
 (hello|red) # Match either hello or red
 .*          # Match any number of characters
){2}         # Match this group twice
()*          # Match the empty string any number of times...

so this matches hello foo hello or red bar red as well.

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

1 Comment

I think use what:)) Apparently no over way. Thx a lot.
2

I'm not sure if it'll solve your problem but here is my solution:

.*(hello.*red|red.*hello).*

Here is an online demo of it with complex explanation of how does it really works: http://regex101.com/r/eL3wA0

Comments

0

It may depend on what you want to do, but I believe regex is an overkill for the task.

How about a simple:

WHERE col LIKE "%red%"
    AND col LIKE "%hello%"

1 Comment

Unfortunately, for my task what not applicable. But thx any way.

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.