Check if any element in a list matches Regex in Python

avatar

Borislav Hadzhiev

Last updated: Aug 10, 2022

banner

Photo from Unsplash

Check if any element in a list matches Regex in Python #

To check if any element in a list matches a regex:

  1. Use a generator expression to iterate over the list.
  2. Use the re.match method to check if each string in the list matches the regex.
  3. Pass the result to the any() function.
main.py
import re prog = re.compile(r'[a-z]+') my_list = ['!', '?', 'abc'] if any((match := prog.match(item)) for item in my_list): print('At least one list item matches regex') print(match.group(0)) # 👉️ 'abc' else: print('No list items match regex') print(prog.match('abc').group(0)) # 👉️ 'abc'

Note that the re.match method only matches a regular expression at the beginning of a string.

The re.search method checks for a match anywhere in the string.

main.py
import re prog = re.compile(r'[a-z]+') my_list = ['!', '?', '???abc???'] # 👇️ using re.search() instead if any((match := prog.search(item)) for item in my_list): print('At least one list item matches regex') print(match.group(0)) # 👉️ 'abc' else: print('no list items match regex') print(prog.search('???abc???').group(0)) # 👉️ 'abc'

We used the re.compile method to compile a regular expression pattern into an object, which can be used for matching using its match() or search() methods.

main.py
import re prog = re.compile(r'[a-z]+') print(prog) # 👉️ re.compile('[a-z]+')

This is more efficient than using re.match or re.search directly because it saves and reuses the regular expression object.

The re.search method looks for the first location in the string where the provided regular expression produces a match.

main.py
import re prog = re.compile(r'[a-z]+') my_list = ['!', '?', '???abc???'] if any((match := prog.search(item)) for item in my_list): print('At least one list item matches regex') print(match.group(0)) # 👉️ 'abc' else: print('no list items match regex') print(prog.search('???abc???').group(0)) # 👉️ 'abc'
If the re.search() method finds a match, it will return a match object, otherwise None is returned.

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The := syntax is called assignment expressions in Python.

main.py
if any((match := prog.search(item)) for item in my_list): print('At least one list item matches regex') print(match.group(0)) # 👉️ 'abc'

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

The re.search and re.match methods return a match object if the string matches the regular expression, otherwise they return None.

If any element in the list matches the regex, the any() method returns True and the if block runs.

If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.