Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple words in any order using regex [duplicate]

Tags:

regex

As the title says , I need to find two specific words in a sentence. But they can be in any order and any casing. How do I go about doing this using regex?

For example, I need to extract the words test and long from the following sentence whether the word test comes first or long comes.

This is a very long sentence used as a test 

UPDATE: What I did not mention in the first part is that it needs to be case insensitive as well.

like image 263
RC1140 Avatar asked Jul 24 '09 11:07

RC1140


People also ask

How do you regex multiple words?

However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.

What is regex multiline?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What does regex (? S match?

i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.


1 Answers

You can use

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

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

like image 111
velop Avatar answered Oct 01 '22 12:10

velop