Open In App

Python | Get matching substrings in string

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The testing of a single substring in a string has been discussed many times. But sometimes, we have a list of potential substrings and check which ones occur in a target string as a substring. Let’s discuss certain ways in which this task can be performed. 
 

Method #1: Using list comprehension 
Using list comprehension is the naive and brute force method to perform this particular task. In this method, we try to get the matching string using the “in” operator and store it in the new list. 

Python3




# Python3 code to demonstrate working of
# Get matching substrings in string
# Using list comprehension
 
# initializing string
test_str = "GfG is good website";
 
# initializing potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial"]
 
# printing original string
print("The original string is : " + test_str)
 
# printing potential strings list
print("The original list is : " + str(test_list))
 
# using list comprehension
# Get matching substrings in string
res = [sub for sub in test_list if sub in test_str]
 
# printing result
print("The list of found substrings : " + str(res))


Output

The original string is : GfG is good website
The original list is : ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings : ['GfG', 'site']

Method #2: Using filter() + lambda 
This task can also be performed using the filter function which performs the task of filtering out the resultant strings that is checked for existence using the lambda function. 

Python3




# Python3 code to demonstrate working of
# Get matching substrings in string
# Using lambda and filter()
 
# initializing string
test_str = "GfG is good website";
 
# initializing potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial"]
 
# printing original string
print("The original string is : " + test_str)
 
# printing potential strings list
print("The original list is : " + str(test_list))
 
# using lambda and filter()
# Get matching substrings in string
res = list(filter(lambda x:  x in test_str, test_list))
 
# printing result
print("The list of found substrings : " + str(res))


Output

The original string is : GfG is good website
The original list is : ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings : ['GfG', 'site']

Method #3 : Using find() method.
find() method returns the position of the string passed as an argument in the given string or else returns -1

Python3




# Python3 code to demonstrate working of
# Get matching substrings in string
 
# initializing string
test_str = "GfG is good website"
 
# initializing potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial" ]
 
# printing original string
print("The original string is : " + test_str)
 
# printing potential strings list
print("The original list is : " + str(test_list))
 
 
# Get matching substrings in string
res=[]
for i in test_list:
    if(test_str.find(i)!=-1 and i not in res):
        res.append(i)
# printing result
print("The list of found substrings : " + str(res))


Output

The original string is : GfG is good website
The original list is : ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings : ['GfG', 'site']

Method #4 : Using re

In this approach using the re (regular expression) module, we import the re module and use its search() function to check for the presence of a substring in the target string. The search() function returns a match object if the substring is found, and None if it is not found. We can use this behavior to filter the list of potential substrings and only keep the ones that are present in the target string.

Python3




# Import the re module
import re
 
# Initialize the target string
test_str = "GfG is good website"
 
# Initialize the list of potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial"]
 
# Print the original target string
print("The original string is : " + test_str)
 
# Print the original list of potential substrings
print("The original list is : " + str(test_list))
 
# Use a list comprehension to filter the list of potential substrings
# and only keep the ones that are found in the target string
res = [sub for sub in test_list if re.search(sub, test_str)]
 
# Print the resulting list of found substrings
print("The list of found substrings : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : GfG is good website
The original list is : ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings : ['GfG', 'site']

The time complexity of the approach using the re module is O(n * m), where n is the length of the target string and m is the number of substrings in the list of potential substrings.

This is because for each substring in the list, we are searching the entire target string to see if the substring is present.

The auxiliary space is O(n), as we are creating a new list of substrings that are found in the target string, and this list will have a length of at most n.

Method #5 : Using index() method.

In python we index ( ) returns matching substring index()” method returns the index of the first occurrence of the substring in the string “index()” method raises a ValueError exception, which is handled by the try-except block.

Python3




# Initializing string
test_str = "GfG is good website"
 
# Initializing potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial"]
 
# Printing original string
print("The original string is:", test_str)
 
# Printing potential substrings list
print("The original list is:", test_list)
 
# Get matching substrings in string
res = []
for sub in test_list:
    try:
        test_str.index(sub)
        res.append(sub)
    except ValueError:
        pass
# Printing result
print("The list of found substrings:", res)


Output

The original string is: GfG is good website
The original list is: ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings: ['GfG', 'site']

The time complexity of this program is O(n * m)

The auxiliary space of this program is O(n)

Method #6 : Using operator.contains() method

  1. Initiate a for loop to traverse list of substrings
  2. Check whether each substring is present in original string using operator.contains()
  3. If present append that substring to output list
  4. Display output list

Python3




# Python3 code to demonstrate working of
# Get matching substrings in string
 
# initializing string
test_str = "GfG is good website"
 
# initializing potential substrings
test_list = ["GfG", "site", "CS", "Geeks", "Tutorial" ]
 
# printing original string
print("The original string is : " + test_str)
 
# printing potential strings list
print("The original list is : " + str(test_list))
 
 
# Get matching substrings in string
res=[]
import operator
for i in test_list:
    if(operator.contains(test_str,i) and i not in res):
        res.append(i)
# printing result
print("The list of found substrings : " + str(res))


Output

The original string is : GfG is good website
The original list is : ['GfG', 'site', 'CS', 'Geeks', 'Tutorial']
The list of found substrings : ['GfG', 'site']

Time Complexity : O(M*N) M -length of string N-length of substring list

Auxiliary Space : O(N) N – number of substrings present in string



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads