Python Regular Expression to Find All Matches in List

If you want to make use of Regular Expressions (RegEx) to find all matches within a Python list, you can make use of the re.findall() method.

Let's take a look at an example:

import re

def re_find_all_matches(pattern, city_list):
    matches = []
    for city in city_list:
        result = re.findall(pattern, city)
    return matches

city_list = ["New York", "Chicago", "Ohio", "Austin", "Texas","New York","Austin"]
pattern = r"Austin"

matches = re_find_all_matches(pattern, city_list)


['Austin', 'Austin']
Regex find all matches in List Python Example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap