Simple string operation:

mywords = ("xxx", "yyy", "zzz")
all(x in mystring for x in mywords)

If word boundaries are relevant (i. e. you want to match zzz but not Ozzzy):

import re
all(re.search(r"\b" + re.escape(word) + r"\b", mystring) for word in mywords)
Answer from Tim Pietzcker on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
3 days ago - Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
Discussions

python - How can I create a regex from a list of words? - Stack Overflow
I have a dict of words (actually I have nested dicts of verb conjugations, but that isn't relevant) and I want to make a regex by combining them. { 'yo': 'hablaba', 'tú': 'hablabas', 'él': ' More on stackoverflow.com
🌐 stackoverflow.com
regex - How to match any string from a list of strings in regular expressions in python? - Stack Overflow
Lets say I have a list of strings, string_lst = ['fun', 'dum', 'sun', 'gum'] I want to make a regular expression, where at a point in it, I can match any of the strings i have in that list, within a More on stackoverflow.com
🌐 stackoverflow.com
python - List of all words matching regular expression - Stack Overflow
Let assume that I have some string: "Lorem ipsum dolor sit amet" I need a list of all words with lenght more than 3. Can I do it with regular expressions? e.g. pattern = re.compile(r'some pattern') More on stackoverflow.com
🌐 stackoverflow.com
November 19, 2011
regex - Python regular expression match multiple words anywhere - Stack Overflow
I'm trying to use python's regular expression to match a string with several words. For example, the string is "These are oranges and apples and pears, but not pinapples or .." The list of words I ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
9

Yes, I believe this is possible.

To get you started, this is how I would break down the problem.

Calculate the root by finding the longest possible string that matches the start of all of the declined values:

>>> root = ''
>>> for c in hablar['yo']:
...     if all(v.startswith(root + c) for v in hablar.itervalues()):
...         root += c
...     else:
...        break
... 
>>> root
'habl'

Whatever's left of the words makes a list of endings.

>>> endings = [v[len(root):] for v in hablar.itervalues()]
>>> print endings
['abas', 'aba', 'abais', 'aba', '\xc3\xa1bamos', 'aban', 'abas']

You may then want to weed out the duplicates:

>>> unique_endings = set(endings)
>>> print unique_endings
set(['abas', 'abais', '\xc3\xa1bamos', 'aban', 'aba'])

Then join these endings together with pipes:

>>> conjoined_endings = '|'.join(unique_endings)
>>> print conjoined_endings
abas|abais|ábamos|aban|aba

Forming the regular expression is a simple matter combining the root and the conjoined_endings string in parentheses:

>>> final_regex = '{}({})'.format(root, conjoined_endings)
>>> print final_regex
habl(abas|abais|ábamos|aban|aba)
2 of 2
3

I think you need to have a less clever approach

>>> x={
...   'yo': 'hablaba',
...   'tú': 'hablabas',
...   'él': 'hablaba',
...   'nosotros': 'hablábamos',
...   'vosotros': 'hablabais',
...   'ellos': 'hablaban',
...   'vos': 'hablabas',
... }
>>> x
{'t\xc3\xba': 'hablabas', 'yo': 'hablaba', 'vosotros': 'hablabais', '\xc3\xa9l': 'hablaba', 'nosotros': 'habl\xc3\xa1bamos', 'ellos': 'hablaban', 'vos': 'hablabas'}
>>> x.values
<built-in method values of dict object at 0x20e6490>
>>> x.values()
['hablabas', 'hablaba', 'hablabais', 'hablaba', 'habl\xc3\xa1bamos', 'hablaban', 'hablabas']
>>> "|".join(x.values())
'hablabas|hablaba|hablabais|hablaba|habl\xc3\xa1bamos|hablaban|hablabas'

If you just join the hash values with an alternation operator then it should do what you want

🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-26.php
Python: Match if two words from a list of words starting with letter 'P' - w3resource
July 22, 2025 - Python Exercises, Practice and Solution: Write a Python program to match if two words from a list of words start with the letter 'P'.
Top answer
1 of 5
64

Join the list on the pipe character |, which represents different options in regex.

string_lst = ['fun', 'dum', 'sun', 'gum']
x="I love to have fun."

print re.findall(r"(?=("+'|'.join(string_lst)+r"))", x)

Output: ['fun']

You cannot use match as it will match from start. Using search you will get only the first match. So use findall instead.

Also use lookahead if you have overlapping matches not starting at the same point.

2 of 5
25

regex module has named lists (sets actually):

#!/usr/bin/env python
import regex as re # $ pip install regex

p = re.compile(r"\L<words>", words=['fun', 'dum', 'sun', 'gum'])
if p.search("I love to have fun."):
    print('matched')

Here words is just a name, you can use anything you like instead.
.search() methods is used instead of .* before/after the named list.

To emulate named lists using stdlib's re module:

#!/usr/bin/env python
import re

words = ['fun', 'dum', 'sun', 'gum']
longest_first = sorted(words, key=len, reverse=True)
p = re.compile(r'(?:{})'.format('|'.join(map(re.escape, longest_first))))
if p.search("I love to have fun."):
    print('matched')

re.escape() is used to escape regex meta-characters such as .*? inside individual words (to match the words literally).
sorted() emulates regex behavior and it puts the longest words first among the alternatives, compare:

>>> import re
>>> re.findall("(funny|fun)", "it is funny")
['funny']
>>> re.findall("(fun|funny)", "it is funny")
['fun']
>>> import regex
>>> regex.findall(r"\L<words>", "it is funny", words=['fun', 'funny'])
['funny']
>>> regex.findall(r"\L<words>", "it is funny", words=['funny', 'fun'])
['funny']
🌐
UI Bakery
uibakery.io › regex-library › match-words-regex-python
Regex match words Python
# Validate words words_pattern = "^\\b(?:\\w|-)+\\b$" re.match(words_pattern, 'word') # Returns Match object re.match(words_pattern, 'pet-friendly') # Returns Match object re.match(words_pattern, 'not a word') # Returns None # Extract words from a string words_extract_pattern = "\\b(?:\\w|-)+\\b" re.findall(words_extract_pattern, 'Hello, world!') # returns ['Hello', 'world']
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions. ... You can add flags to the pattern when using regular expressions. A special sequence is a \ followed by one of the characters in the list below, and has a special meaning:
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex list
Python regex list - Spark By {Examples}
May 31, 2024 - Using list comprehension, we iterate over each word in the words list and check if it matches the specified pattern using re.match(). If a word matches the pattern, it is included in the filtered_words list. ... The re.sub() method can be so useful in scenarios where you want to replace elements in a list. Let us look at an example: import re # list of strings to modify strings = ["Python123", "PySpark456", "Scala789", "MongoDB123"] # pattern to match and replace pattern = r'\d+' # Matches one or more digits # replacement string replacement = "NUM" # iterate over the list and replace matching elements using re.sub() modified_strings = [re.sub(pattern, replacement, string) for string in strings] # print the modified list print(modified_strings)
🌐
Regex Tester
regextester.com › 93690
Find any word in a list of words - Regex Tester/Debugger
Url checker with or without http:// or https:// Match string not containing string Check if a string only contains numbers Match elements of a url Match an email address Validate an ip address Match or Validate phone number Match html tag Match dates (M/D/YY, M/D/YYY, MM/DD/YY, MM/DD/YYYY) Empty String Checks the length of number and not starts with 0 Match a valid hostname Not Allowing Special Characters Validate datetime Person Name string between quotes + nested quotes Match brackets Url match a wide range of international phone number Match IPv6 Address · Regex Tester isn't optimized for mobile devices yet.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex search list
Python regex search list - Spark By {Examples}
May 31, 2024 - In this tutorial, we will be exploring how we can use regex in Python to search through a list. ... The question that might pop into your mind is why in the first place would you even bother to use regex to search a list? Below are the reasons why you should use regex for your list search: ... In this section, I will demonstrate how you can use the re.search() method to search a list. Here is an example: import re # list of names names = ["Guido Van Rossum", "Brendan Eich", "Rasmus Lerdorf", "Bjarne Stroustrup", "Dennis Ritchie"] # regex pattern to search for names starting with "B" pattern = r"^B\w+" # iterate over the list and search for matching names for name in names: # calling the search() method match = re.search(pattern, name) if match: print("Match found:", name)
🌐
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.
🌐
Medium
medium.com › quantrium-tech › extracting-words-from-a-string-in-python-using-regex-dac4b385c1b8
Extracting Words from a string in Python using RegEx
October 6, 2020 - QUANTRIUM GUIDES Extracting Words from a string in Python using the “re” module Extract word from your text data using Python’s built in Regular Expression Module Regular expression (RegEx) is …
🌐
Guru99
guru99.com › home › python › python regex: re.match(), re.search(), re.findall() with example
Python RegEx: re.match(), re.search(), re.findall() with Example
August 13, 2025 - But if a match is found in some other line, the Python RegEx Match function returns null. For example, consider the following code of Python re.match() function. The expression “w+” and “\W” will match the words starting with letter ‘g’ and thereafter, anything which is not started with ‘g’ is not identified. To check match for each element in the list ...
🌐
ZetCode
zetcode.com › python › regularexpressions
Python regular expressions - using regular expressions in Python
We look for matches with regex functions. The match, fullmatch, and search functions return a match object if they are successful. Otherwise, they return None. The match function returns a match object if zero or more characters at the beginning of string match the regular expression pattern. ... #!/usr/bin/python import re words = ('book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook') pattern = re.compile(r'book') for word in words: if re.match(pattern, word): print(f'The {word} matches')
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
The re.findall(pat, str) function finds all matches of a pattern in a string and returns them as a list of strings or tuples, depending on whether the pattern contains capturing groups. Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python.