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.

Answer from vks on Stack Overflow
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']
🌐
Stack Abuse
stackabuse.com › bytes › check-if-elements-in-list-matches-a-regex-in-python
Check if Elements in List Matches a Regex in Python
September 14, 2023 - Link: For more information on using regex in Python, check out our article, Introduction to Regular Expressions in Python · To check if any element in a list matches a regular expression, you can use a loop to iterate over the list and the re module's match() function to check each element. Here's an example: import re # List of strings list_of_strings = ['apple', 'banana', 'cherry', 'date'] # Regular expression pattern for strings starting with 'a' pattern = '^a' for string in list_of_strings: if re.match(pattern, string): print(string, "matches the pattern")
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
1 week ago - Usually patterns will be expressed ... this raw string notation. It is important to note that most regular expression operations are available as module-level functions and methods on compiled regular expressions. The functions are shortcuts that don’t require you to compile a regex object first, but miss some fine-tuning parameters. ... The third-party regex module, which has an API compatible with the standard library re module, but offers additional ...
🌐
Reddit
reddit.com › r/regex › how....do you apply regex to a list in python?
r/regex on Reddit: How....do you apply regex to a list in python?
April 21, 2020 -

So I spent a few days figuring out my beautiful regex found here to parse whatsapp messages. It through the text and puts it into groups, but now....I just don't know how to use it to pull out the data.

I am in google collab pulling my whatsapp raw text into a list like this:

def read_file(file):
'''Reads Whatsapp text file into a list of strings'''
x = open(file,'r', encoding = 'utf-8') #Opens the text file into variable x but the variable cannot be explored yet
y = x.read() #By now it becomes a huge chunk of string that we need to separate line by line
content = y.splitlines() #The splitline method converts the chunk of string into a list of strings
return content
chat = read_file('18042020_cut.txt')

Cool. so like...what do I do now?

I tried:

content = re.search('[(?P<date>\d{2}/\d{2}/\d{4}),\s(?P<time>\d{1,2}:\d{2}:\d{2}.{3})]\s(?P<sender>[:]*):\s(?P<message>.+|\n+(?!)[\d{2}/\d{2}/\d{4})', chat).group('Content')

🌐
Reddit
reddit.com › r/learnpython › how to create a regular expression from list of input strings?
r/learnpython on Reddit: How to create a regular expression from list of input strings?
July 10, 2024 -

I am working on a project where I have a input list of filenames and I want to compute a regular expression that is as precise as possible and validate against all elements of the input list of file names. Is there a regular expression library or method to solve this? I've tried looking online but I only find results regarding validating a list of string inputs against an already defined input regular expression which is the opposite of what I am trying to do.

I have a DB with 2 tables that is being used to correlate excel tables/ ranges to specific files. One DB table has the data source name and file name pattern to the name of the file that data source is saved in. The other table has all the excel tables/ ranges that can be found in each data source. By joining these two tables, I can get a list of all data sources, the excel ranges the data source includes and the file name pattern they come from.

I want to fill the database's file patterns using existing XML files which store actual file names that had been used in the past to store the data. Processing these XML files, I can determine that, for example, data source A has in the past had file names of BAB.xls, CAB.xls, and DAB.xls.

I want to try and create a program that can take the list ['BAB.xls', 'CAB.xls', 'DAB.xls'] and return a regular expression like /.AB\.xls/.

🌐
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:
🌐
GeeksforGeeks
geeksforgeeks.org › python-check-if-string-matches-regex-list
Python | Check if string matches regex list | GeeksforGeeks
April 23, 2023 - In this, we create a new regex string by joining all the regex list and then match the string against it to check for match using match() with any of the element of regex list. ... # Python3 code to demonstrate working of # Check if string matches ...
Find elsewhere
🌐
Quora
quora.com › What-is-matching-a-list-of-regex-to-every-string-in-a-list-one-by-one-Python
What is matching a list of regex to every string in a list one by one (Python)? - Quora
Answer (1 of 2): The answer is (as usual) you just do that. Suppose I have a list of regular expression objects made with re.compile called pats and a list of strings called strs. Then: [code]for s in strs: x = [p.match(s) for p in pats] ... do something with x [/code]x is a list of Nones or ...
🌐
Quora
quora.com › Is-there-a-way-in-Python-to-apply-a-list-of-regex-patterns-that-are-stored-in-a-list-to-a-single-string
Is there a way in Python to apply a list of regex patterns that are stored in a list to a single string? - Quora
Answer: You can combine the map function along with re to do this. [code]import re my_string = 'Allwin' patterns = [r'^A', r'.{6}'] print(list(map(lambda pattern: True if re.match(pattern, my_string) else False, patterns))) # output: [True, True] [/code]I have two patterns. 1. r’A’ - This pat...
Top answer
1 of 6
5

Firstly, your regex seems to not work properly. The Key field should have values which could include f, right? So its group should not be ([0-9A-Ea-e]+) but instead ([0-9A-Fa-f]+). Also, it is a good - actually, a wonderful - practice to prefix the regex string with r when dealing with regexes because it avoids problems with \ escaping characters. (If you do not understand why to do it, look at raw strings)

Now, my approach to the problem. First, I would create a regex without pipes:

>>> regex = r"(Key):[\s]*([0-9A-Fa-f]+)[\s]*" \
...     r"(Index):[\s]*([0-9]+)[\s]*" \
...     r"(Field 1):[\s]*([0-9]+)[\s]*" \
...     r"(Field 2):[\s]*([0-9 A-Za-z]+)[\s]*" \
...     r"(Field 3):[\s]*([-+]?[0-9]+)[\s]*"

With this change, the findall() will return only one tuple of found groups for an entire line. In this tuple, each key is followed by its value:

>>> re.findall(regex, line)
[('Key', 'af12d9', 'Index', '0', 'Field 1', '1234', 'Field 2', '1234 Ring ', 'Field 3', '-10')]

So I get the tuple...

>>> found = re.findall(regex, line)[0]
>>> found
('Key', 'af12d9', 'Index', '0', 'Field 1', '1234', 'Field 2', '1234 Ring ', 'Field 3', '-10')

...and using slices I get only the keys...

>>> found[::2]
('Key', 'Index', 'Field 1', 'Field 2', 'Field 3')

...and also only the values:

>>> found[1::2]
('af12d9', '0', '1234', '1234 Ring ', '-10')

Then I create a list of tuples containing the key and its corresponding value with zip() function:

>>> zip(found[::2], found[1::2])
[('Key', 'af12d9'), ('Index', '0'), ('Field 1', '1234'), ('Field 2', '1234 Ring '), ('Field 3', '-10')]

The gran finale is to pass the list of tuples to the dict() constructor:

>>> dict(zip(found[::2], found[1::2]))
{'Field 3': '-10', 'Index': '0', 'Field 1': '1234', 'Key': 'af12d9', 'Field 2': '1234 Ring '}

I find this solution the best, but it is indeed a subjective question in some sense. HTH anyway :)

2 of 6
1

OK, with help of brandizzi, I have found THE answer to this question.

Solution:

listconfig = []
for line in list_of_strings:
    matched = re.search(r"Key:[\s]*(?P<key>[0-9A-Fa-f]+)[\s]*" \ 
                        r"(Index:[\s]*(?P<index>[0-9]+)[\s]*)?" \ 
                        r"(Field 1:[\s]*(?P<field_1>[0-9]+)[\s]*)?" \ 
                        r"(Field 2:[\s]*(?P<field_2>[0-9 A-Za-z]+)[\s]*)?" \ 
                        r"(Field 3:[\s]*(?P<field_3>[-+]?[0-9]+)[\s]*)?", line) 
    if matched:
        print matched.groupdict()
        listconfig.append(matched.groupdict())
🌐
TutorialsPoint
tutorialspoint.com › python-check-if-a-string-matches-regex-list
Python - Check if a string matches regex list
August 2, 2023 - Predefined sets of characters represented by such special sequences beginning with '\' are listed below − · Python's re module provides useful functions for finding a match, searching for a pattern, and substitute a matched string with other string etc.
🌐
Pyladiespdx
pyladiespdx.github.io › listcomps
Get Comfortable with List Comprehensions and Regex | Pyladiespdx
Searching and matching works by looking for the complete pattern in each string; running through from start to finish; and stopping as soon as a match is found, returning the match object (or None if not found). ... \ : inhibit the uniqueness of a character that is otherwise considered a meta-character (i.e., \ . for period; \ \ for slash; $ for a dollar sign) ? : match zero or one occurrence of the pattern to the left (i.e., may or may not appear in pattern) - : if between two [], indicates a range of digits or alphabetic chars; will be interpreted as a literal if appearing first or last inside [].
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex list
Python regex list - Spark By {Examples}
May 31, 2024 - The code iterates through each language in the list and uses the re.search() function to check if the pattern is present in the language string. If a match is found, it prints a message indicating that the pattern was found in the specific language. ... Another way for searching patterns within elements in a list is by using the re.findall() method. Here is an example: import re # the list of languages to search in languages = ["JavaScript", "Java", "Python", "C", "C++", "C#"] # the pattern to search for pattern = r"va" # assigning the method return results to matches variable matches = re.findall(pattern, " ".join(languages)) # prints the list of the matches print(matches)
🌐
Coderanch
coderanch.com › t › 755566 › languages › Matching-list-regex-string-list
Matching a list of regex to every string in a list one by one [Python] (Jython/Python forum at Coderanch)
November 2, 2022 - adina nadeem wrote:As soon as a match is found, I want to no longer look in the regex list for further matches. I believe that's what's happening. For each of the three strings in str, it's looking at patterns until it finds a pattern that matches - which seems to be the third pattern, which can be found in all three strings.
🌐
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)