Python | Get matching substrings in string
Last Updated :
24 Mar, 2023
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
test_str = "GfG is good website" ;
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is : " + test_str)
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if sub in test_str]
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
test_str = "GfG is good website" ;
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is : " + test_str)
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x in test_str, test_list))
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
test_str = "GfG is good website"
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is : " + test_str)
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if (test_str.find(i)! = - 1 and i not in res):
res.append(i)
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 re
test_str = "GfG is good website"
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is : " + test_str)
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if re.search(sub, test_str)]
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']
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
test_str = "GfG is good website"
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is:" , test_str)
print ( "The original list is:" , test_list)
res = []
for sub in test_list:
try :
test_str.index(sub)
res.append(sub)
except ValueError:
pass
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
- Initiate a for loop to traverse list of substrings
- Check whether each substring is present in original string using operator.contains()
- If present append that substring to output list
- Display output list
Python3
test_str = "GfG is good website"
test_list = [ "GfG" , "site" , "CS" , "Geeks" , "Tutorial" ]
print ( "The original string is : " + test_str)
print ( "The original list is : " + str (test_list))
res = []
import operator
for i in test_list:
if (operator.contains(test_str,i) and i not in res):
res.append(i)
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
Similar Reads
Python - Get all substrings of given string
A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read
Python - Extract string between two substrings
The problem is to extract the portion of a string that lies between two specified substrings. For example, in the string "Hello [World]!", if the substrings are "[" and "]", the goal is to extract "World". If the starting or ending substring is missing, handle the case appropriately (e.g., return an
3 min read
Python | Count overlapping substring in a given string
Given a string and a sub-string, the task is to get the count of overlapping substring from the given string. Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider thi
2 min read
Get Second Occurrence of Substring in Python String
We are given a string and a substring, and our task is to find the index of the second occurrence of that substring within the string. This means we need to identify not just if the substring exists, but where it appears for the second time. For example, if we have a string like "hello world, hello
2 min read
Python - All occurrences of substring in string
A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String. Using re.finditer()re.finditer() returns an iterator yielding match object
3 min read
Python - Remove after substring in String
Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Extract List of Substrings in List of Strings in Python
Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c
3 min read
Create List of Substrings from List of Strings in Python
In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th
3 min read
Python - Ways to Count Number of Substring in String
Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho
2 min read
Python - String till Substring
When working with Python strings, we may encounter a situation where we need to extract a portion of a string that starts from the beginning and stops just before a specific substring. Let's discuss certain ways in which we can do this. Using split()The split() method is a simple and efficient way t
3 min read