Multiple words in any order using regex

I assume (always dangerous) that you want to find whole words, so "test" would match but "testy" would not. Thus the pattern must search for word boundaries, so I use the "\b" word boundary pattern.

/(?i)(\btest\b.*\blong\b|\blong\b.*\btest\b)/

Use a capturing group if you want to extract the matches: (test)|(long) Then depending on the language in use you can refer to the matched group using $1 and $2, for example.


without knowing what language

 /test.*long/ 

or

/long.*test/

or

/test/ && /long/

You can use

(?=.*test)(?=.*long)

Source: MySQL SELECT LIKE or REGEXP to match multiple words in one record

Tags:

Regex