Sometimes, while working with Python, we can have a problem we have list of regex and we need to check a particular string matches any of the available regex in list. Let’s discuss a way in which this task can be performed.
Method : Using join regex + loop + re.match()
This task can be performed using combination of above functions. 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 regex list # Using join regex + loop + re.match() import re # initializing list test_list = ["gee*", "gf*", "df.*", "re"] # printing list print("The original list : " + str(test_list)) # initializing test_str test_str = "geeksforgeeks" # Check if string matches regex list # Using join regex + loop + re.match() temp = '(?:% s)' % '|'.join(test_list) res = Falseif re.match(temp, test_str): res = True # Printing result print("Does string match any of regex in list ? : " + str(res)) |
The original list : ['gee*', 'gf*', 'df.*', 're'] Does string match any of regex in list ? : True
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Python | Check if suffix matches with any string in given list
- Python | Find all close matches of input string from a list
- How to check if a string starts with a substring using regex in Python?
- Python - Check if String Contain Only Defined Characters using Regex
- Python - Extract Indices of substring matches
- Python regex | Check whether the input is Floating point number or not
- Python Regex to extract maximum numeric value from a string
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- The most occurring number in a string using Regex in python
- Python Regex - Program to accept string starting with vowel
- Python Regex | Program to accept string ending with alphanumeric character
- Split a String into columns using regex in pandas DataFrame
- Python | Convert List of String List to String List
- Pattern matching in Python with Regex
- Regex in Python to put spaces between words starting with capital letters
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python | Parse a website with regex and urllib
- Name validation using IGNORECASE in Python Regex
- Verbose in Python Regex
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

