Check if any element in a List matches Regex in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# 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'

check if any element in list matches regex

The code for this article is available on GitHub

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

# Check if any element in a list matches Regex using re.search()

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'

check if any element in list matches regex with re search

The code for this article is available on GitHub

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'

how re search method works

The code for this article is available on GitHub
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'
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev