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 = False
if 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
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.
import fnmatch
# 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 filter + fnmatch res = bool ( list ( filter ( lambda x: fnmatch.fnmatch(test_str, x), test_list)))
# 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
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.
import fnmatch
# 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 list comprehension + fnmatch res = any (fnmatch.fnmatch(test_str, pattern) for pattern in test_list)
# 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
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.