Regex for word followed by another word

I want to find a regex command that I can run on the command line that will find a whole word followed by another whole word (that I specify in the command).

What I am looking to do is also include a file extension (like .txt) in the command such that it only runs the regex on files with that extension recursively in a directory structure.

I know how to use the $find command with -exec but have not been able to assemble the regex.

$find . -name '*.txt' -exec <regex command> {} \;

Something like this?

find ./ -name "*.txt" -exec grep 'word1 word2' {} \;

This almost gets me there, tried that, but it does not give me word boundaries.

Give an example of a string that's supposed to match but doesn't, please.

Here are two random words that I just thought up.

the word "find" and the word "me".

So I want to search all .txt files recursively that have the whole word "me" followed by the word "find".

so "find mellow yellow" would fail the regex as does any other word ending in "find".

so I need "<space>find<one or more spaces or any characters>me<space>"

thanks for your help.

Why wouldn't "find me" match "find mellow yellow"? It should. I'm guessing you've given the exact opposite -- a string that shouldn't match, but does.

You've broken down what you want almost exactly though, so:

egrep "(^| )find( | .* )me( |$)"

This should insist on 'find' and 'me' being whole words, and find them even when they're on the beginning or end of lines. It must find them both on the same line though.

There are differences in Regex engines. Corona used egrep, so you have to use egrep as well.

Finding exact words separated by arbitrary numbers of whitespace can be done like this:

$ export word1=find
$ export word2=me
$ regex2wd2=$(printf "'%s[   ]+%s'" $word1 $word2)
$ echo "find   me" | egrep "$regex2d"
find   me

the [ ] thing has one space character and one tab character inside the [ ] characters.
Note the single quote characters embedded in the printf format string. They prevents the shell in this case bash, from messing up the regex by trying to interpret goofy characters when you use the string as a regex.