You've got a few problems there.

First, matches are case-sensitive unless you use the IGNORECASE/I flag to ignore case. So, 'AND' doesn't match 'and'.

Also, unless you use the VERBOSE/X flag, those spaces are part of the pattern. So, you're checking for 'AND ', not 'AND'. If you wanted that, you probably wanted spaces on each side, not just those sides (otherwise, 'band leader' is going to match…), and really, you probably wanted \b, not a space (otherwise a sentence starting with 'And another thing' isn't going to match).

Finally, if you think you need .* before and after your pattern and $ and ^ around it, there's a good chance you wanted to use search, findall, or finditer, rather than match.

So:

>>> s = "These are oranges and apples and pears, but not pinapples or .."
>>> r = re.compile(r'\bAND\b | \bOR\b | \bNOT\b', flags=re.I | re.X)
>>> r.findall(s)
['and', 'and', 'not', 'or']

Debuggex Demo

Answer from abarnert on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch05s02.html
5.2. Find Any of Multiple Words - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - Any regex metacharacters within the accepted words are escaped // with a backslash before searching. function matchWords(subject, words) { var regexMetachars = /[(){[*+?.\\^$|]/g; for (var i = 0; i < words.length; i++) { words[i] = words[i].replace(regexMetachars, "\\$&"); } var regex = new RegExp("\\b(?:" + words.join("|") + ")\\b", "gi"); return subject.match(regex) || []; } matchWords(subject, ["one","two","three"]); // Returns an array with four matches: ["One","two","one","three"]
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
Discussions

Multiple words in any order using regex - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - Match multiple words in any order via one regex - Stack Overflow
You asked us not to ask, but I just have to: When finding the values for multiple different keys, wouldn't it be very helpful to know which keys those values belong to? ... You may leverage a non-capturing alternation group to match either VERSION or FREQ (optionally preceded with a word boundary, ... More on stackoverflow.com
🌐 stackoverflow.com
November 13, 2017
python - Match a list of possible words in a string in any order - Stack Overflow
I know this question sounds similar to others, but see my notes below on those solutions. I need a regex to use in Python to search for specific words in any order in a string. What I want to do is... More on stackoverflow.com
🌐 stackoverflow.com
January 6, 2021
python - Match regex in any order - Stack Overflow
I want to check for complex password with regular expression. It should have 1 number 1 uppercase and one lowercase letter, not in specific order. So i though about something like this: m = re.se... More on stackoverflow.com
🌐 stackoverflow.com
🌐
py4u
py4u.org › blog › python-regular-expression-match-multiple-words-anywhere
How to Match Multiple Words Anywhere in a String with Python Regular Expressions (Regardless of Order or Position)
Use Word Boundaries (\b): Avoid partial matches (e.g., \bapple\b vs. apple to exclude "apples"). Escape Special Characters: Always use re.escape() if words contain regex metacharacters (+, ., *, etc.). ... Empty strings (text = "" → should return False). Words not present (text = "apple banana" → missing "cherry" → False). Mixed order and punctuation. Use Raw Strings (r''): Prevents Python from interpreting backslashes (e.g., r'\b' vs.
🌐
Quora
quora.com › How-can-I-write-a-Python-regex-to-match-multiple-words
How to write a Python regex to match multiple words - Quora
Answer (1 of 2): > Q: How can I write a Python regex to match multiple words? An answer is provided in Hasi Syed's answer to How can I write a Python regex to match multiple words?. I would like to stress the following: * beware, regular expressions are by default greedy, that is, they match a...
🌐
TutorialsPoint
tutorialspoint.com › How-to-write-a-Python-regular-expression-to-match-multiple-words-anywhere
How to write a Python regular expression to match multiple words anywhere?
February 20, 2020 - import re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)
Find elsewhere
🌐
regex101
regex101.com › library › fA2RAC
regex101: Matching multiple words in any order in the same string
This regex will match any Youtube video ID thrown at it and return one capturing group containing the ID.Submitted by Jacob Overgaard
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
2 weeks ago - The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups.
🌐
CopyProgramming
copyprogramming.com › howto › how-to-match-multiple-words-in-regex
Regex: Matching Multiple Words Using Regular Expressions
April 5, 2023 - This pattern will match any letter, ... as described in the Syntax Class Table. Multiple words in any order using regex, You're better off with programming the software to be case insensitive....
🌐
CopyProgramming
copyprogramming.com › howto › multiple-words-in-any-order-using-regex
Regex: Regex for Matching Multiple Words in Any Order
April 2, 2023 - See the Python demo. ... Unfortunately, regex cannot match in the specific order you desire. One workaround is to first execute the section before the | symbol, followed by the section after it. Alternatively, you can sort the output after executing the regex.
🌐
LearnByExample
learnbyexample.github.io › py_regular_expressions › alternation-and-grouping.html
Alternation and Grouping - Understanding Python re(gex)?
>>> words = 'lion elephant are rope not' # span shows the start and end+1 index of the matched portion # match shows the text that satisfied the search criteria >>> re.search(r'on', words) &LTre.Match object; span=(2, 4), match='on'> >>> re.search(r'ant', words) &LTre.Match object; span=(10, 13), match='ant'> # starting index of 'on' < index of 'ant' for the given string input # so 'on' will be replaced irrespective of order # count optional argument here restricts no.
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular Expression HOWTO — Python 3.14.3 documentation
Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like.
🌐
CopyProgramming
copyprogramming.com › howto › regex-to-match-multiple-words-in-a-string
Regex: Matching several words within a string using regular expressions
May 29, 2023 - Using Regular Expressions to Match Multiple Words in Any Order [Duplicate], Matching Multiple Words Enclosed in Braces with Underscore Delimiters, while Ignoring Characters at String's Start/End, Creating a Python Regular Expression to Locate Multiple Words in Any Position, Searching for Multiple ...
🌐
Reddit
reddit.com › r/regex › match two words anywhere in text
r/regex on Reddit: Match two words anywhere in text
April 19, 2024 -

I'm very new to RegEx, but I'm trying to learn.

I'm looking to match two words which can be present anywhere in a body of text, separated by multiple line breaks/characters.

For example, let's say I want to match the word "apple" and "dog". It should match only if both words are present somewhere in the text. It can also be in any order.

It could be in something like:

Testing

Testing 2

Dog

Testing 3

Apple

I've tried things like: (apple)(dog) (apple)((.|\n)*)dog

(apple)((.|\n)*)dog works, but doesn't support the "any order"

What am I missing?

🌐
freeCodeCamp
forum.freecodecamp.org › t › regex-to-match-string-containing-two-words-in-any-order › 333791
Regex to match string containing two words in any order - The freeCodeCamp Forum
December 16, 2019 - Hi guys, I want to search a string with two , maybe three words , maybe more … But the basic idea is this: If I have a string like "Hello my name is Jack and this is Matt." And I have a working regex like this: ^(?=.*\bjack\b)(?=.*\bmatt\b).*$ However, it’s hard-coded.
🌐
Reddit
reddit.com › r/learnpython › regular expressions: how can i match multiple groups in any order multiple times?
r/learnpython on Reddit: Regular expressions: How can I match multiple groups in any order multiple times?
June 2, 2022 -

Let's say I have the following list of strings that I want to match:

['ab', 'cde', 'abb', 'efbc']

Is there a way to write a single RE that will match any string that contains any amount of these strings in any order?

So for example, all of these would be valid matches:

abcdeabb
ababbefbc
efbccdeab
cdecdeabefbcab
ababababab

If you don't feel like working it out, I'd appreciate any general guidance as well, such as what I need to look into. Perhaps look-ahead and/or look-behind? I'm not familiar with those.

Thanks!