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
import re
test_list = [ "gee*" , "gf*" , "df.*" , "re" ]
print ( "The original list : " + str (test_list))
test_str = "geeksforgeeks"
temp = '(?:% s)' % '|' .join(test_list)
res = False
if re.match(temp, test_str):
res = True
print ( "Does string match any of regex in list ? : " + str (res))
|
Output
The original list : ['gee*', 'gf*', 'df.*', 're']
Does string match any of regex in list ? : True
Another approach could be using a regular expression library like ‘fnmatch’ which can use ‘filter’ function to check if the string matches any of the regex in the list, this approach would have a time complexity of O(n) where n is the number of regex in the list and a space complexity of O(1) as it does not require any additional data structures.
Python3
import fnmatch
test_list = [ "gee*" , "gf*" , "df.*" , "re" ]
print ( "The original list : " + str (test_list))
test_str = "geeksforgeeks"
res = bool ( list ( filter ( lambda x: fnmatch.fnmatch(test_str, x), test_list)))
print ( "Does string match any of regex in list ? : " + str (res))
|
Output
The original list : ['gee*', 'gf*', 'df.*', 're']
Does string match any of regex in list ? : True
The complexity analysis for this approach depends on the size of the input list and the length of the input string.
- The filter function is used to filter elements from the test_list that match the given regular expression (in this case, fnmatch.fnmatch(test_str, x)). The time complexity of the filter function is O(n), where n is the number of elements in the input list.
- The fnmatch.fnmatch function is used to match the input string with each regular expression in the filtered list. The time complexity of this function is O(k), where k is the length of the input string.
- The list function is used to convert the filtered result into a list. The time complexity of this function is O(n), where n is the number of elements in the filtered list.
Overall, the time complexity of this approach is O(n*k) where n is the number of elements in the input list and k is the length of the input string.
Auxiliary Space is O(n)
Method 3 : using fnmatch library
- Import the fnmatch library.
- Use a list comprehension to create a list of Boolean values indicating whether each regular expression in test_list matches test_str using fnmatch.fnmatch().
- Check if True exists in the list created in step 2 using the any() function.
- Print the result.
Python3
import fnmatch
test_list = [ "gee*" , "gf*" , "df.*" , "re" ]
print ( "The original list : " + str (test_list))
test_str = "geeksforgeeks"
res = any (fnmatch.fnmatch(test_str, pattern) for pattern in test_list)
print ( "Does string match any of regex in list ? : " + str (res))
|
Output
The original list : ['gee*', 'gf*', 'df.*', 're']
Does string match any of regex in list ? : True
Time complexity: O(n), where n is the number of regular expressions in test_list.
Auxiliary space: O(1) for res, O(n) for the list comprehension created in step 2.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Apr, 2023
Like Article
Save Article