Python – Extract string between two substrings
Given a string and two substrings, write a Python program to extract the string between the found two substrings.
Example:
Input : test_str = “Gfg is best for geeks and CS”, sub1 = “is”, sub2 = “and”
Output: best for geeks
Explanation: best for geeks is between is and ‘and’Input : test_str = “Gfg is best for geeks and CS”, sub1 = “for”, sub2 = “and”
Output: geeks
Explanation: geeks is between for and ‘and’
Method #1: Using index() + loop to extract string between two substrings
In this, we get the indices of both the substrings using index(), then a loop is used to iterate within the index to find the required string between them.
Python3
# Python3 code to demonstrate working # of Extract string between 2 substrings # Using loop + index() # initializing string test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" # getting index of substrings idx1 = test_str.index(sub1) idx2 = test_str.index(sub2) res = '' # getting elements in between for idx in range (idx1 + len (sub1) + 1 , idx2): res = res + test_str[idx] # printing result print ( "The extracted string : " + res) |
Output:
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using index() + string slicing to extract string between two substrings
Similar to the above method, just the task of slicing is performed using string slicing for providing a much compact solution.
Python3
# Python3 code to demonstrate working # of Extract string between 2 substrings # Using index() + string slicing # initializing string test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" # getting index of substrings idx1 = test_str.index(sub1) idx2 = test_str.index(sub2) # length of substring 1 is added to # get string from next character res = test_str[idx1 + len (sub1) + 1 : idx2] # printing result print ( "The extracted string : " + res) |
Output:
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Method #3: Using find()+ string slicing to extract string between two substrings
find() method returns the position of string passed as argument or else returns -1.Finally slice the string with the positions.
Python3
# Python3 code to demonstrate working # of Extract string between 2 substrings # Using find() + string slicing # initializing string test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" # getting index of substrings idx1 = test_str.find(sub1) idx2 = test_str.find(sub2) # length of substring 1 is added to # get string from next character res = test_str[idx1 + len (sub1) + 1 : idx2] # printing result print ( "The extracted string : " + res) |
Output:
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Method #4: Using replace() and split() to extract string between two substrings
Here we are using the replace and splitting the original string till we get the desired substrings at the corner and then extract it.
Python3
# Python3 code to demonstrate working # of Extract string between 2 substrings # initializing string test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" test_str = test_str.replace(sub1, "*" ) test_str = test_str.replace(sub2, "*" ) re = test_str.split( "*" ) res = re[ 1 ] # printing result print ( "The extracted string : " + res) |
Output:
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Method #5: Using regex to extract string between two substrings
Here we are using a built-in library i.e. we are using the regex to extract string between two substrings.
Python3
import re test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" s = str (re.escape(sub1)) e = str (re.escape(sub2)) # printing result res = re.findall(s + "(.*)" + e,test_str)[ 0 ] print ( "The extracted string : " + res) |
Output:
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Method 6: Using split() and join() to extract string between two substrings
Step-by-Step Approach:
- Initialize the input string “test_str” and the two substrings “sub1” and “sub2“.
- Use the split() function to split the input string at the position of the two substrings, which returns a list with three elements: the substring before sub1, the substring between sub1 and sub2, and the substring after sub2.
- Use the join() function to concatenate the second element of the list (which is the substring between sub1 and sub2) into a string.
- Print the extracted string.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working # of Extract string between 2 substrings # Using split() and join() # initializing string test_str = "Gfg is best for geeks and CS" # printing original string print ( "The original string is : " + str (test_str)) # initializing substrings sub1 = "is" sub2 = "and" # getting elements in between using split() and join() res = ''.join(test_str.split(sub1)[ 1 ].split(sub2)[ 0 ]) # printing result print ( "The extracted string : " + res) |
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string, as we create a new string “res” to store the extracted string.
Please Login to comment...