Given a list of strings. The task is to split the string by the given list of strings.
Input : test_str = ‘geekforgeeksbestforgeeks’, sub_list = [“best”]
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’]
Explanation : “best” is extracted as different list element.Input : test_str = ‘geekforgeeksbestforgeeksCS’, sub_list = [“best”, “CS”]
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’, “CS”]
Explanation : “best” and “CS” are extracted as different list element.
Method : Using re.split() + | operator
In this, we perform the task of split using regex split() with | operator to check for all the words that need to be put separately.
# Python3 code to demonstrate working of # Separate specific Strings # Using re.split() + | operator import re
# initializing string test_str = 'geekforgeeksisbestforgeeks'
# printing original String print ( "The original string is : " + str (test_str))
# initializing list words sub_list = [ "best" ]
# regex to for splits() # | operator to include all strings temp = re.split(rf "({'|'.join(sub_list)})" , test_str)
res = [ele for ele in temp if ele]
# printing result print ( "The segmented String : " + str (res))
|
The original string is : geekforgeeksisbestforgeeks The segmented String : ['geekforgeeksis', 'best', 'forgeeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method : Using replace(),split() methods
Approach
- Initiate a for loop to traverse list of strings
- Replace the string in original string by appending “*” front and back
- Finally split the string with * as parameter
- This will return a list, display the list
# Python3 code to demonstrate working of # Separate specific Strings # initializing string test_str = 'geekforgeeksisbestforgeeks'
# printing original String print ( "The original string is : " + str (test_str))
# initializing list words sub_list = [ "best" ]
for i in sub_list:
test_str = test_str.replace(i, "*" + i + "*" )
res = test_str.split( "*" )
# printing result print ( "The segmented String : " + str (res))
|
The original string is : geekforgeeksisbestforgeeks The segmented String : ['geekforgeeksis', 'best', 'forgeeks']
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 3 : Use the re module
step by step approach :
- The program starts by importing the “re” module. This module provides regular expression operations in Python.
- The program then initializes a string variable named “test_str” with the value “geekforgeeksisbestforgeeks”.
- The program then prints the original string using the “print” statement and concatenating the original string with a string literal.
- The program initializes a list variable named “sub_list” with the value [“best”].
- The program creates a regular expression pattern by joining the elements of the “sub_list” with a pipe symbol “|”. The pipe symbol is used to separate the substrings in the pattern. The resulting pattern is assigned to the variable “pattern”.
- The program then splits the original string “test_str” using the regular expression pattern stored in the variable “pattern”. The resulting segments are stored in the variable “res”.
- Finally, the program prints the segmented string using the “print” statement and concatenating the segmented string with a string literal.
import re
# initializing string test_str = 'geekforgeeksisbestforgeeks'
# printing original String print ( "The original string is : " + str (test_str))
# initializing list words sub_list = [ "best" ]
# add a '|' between the substrings to create the pattern for splitting pattern = '|' .join(sub_list)
# split the string using the pattern res = re.split(pattern, test_str)
# printing result print ( "The segmented String : " + str (res))
|
The original string is : geekforgeeksisbestforgeeks The segmented String : ['geekforgeeksis', 'forgeeks']
Time complexity: The replace() method has a time complexity of O(n), where n is the length of the string.
Auxiliary space: The original string is replaced with a new string, which has a space complexity of O(n). The segmented string is stored in a list, which has a space complexity of O(m), where m is the number of substrings. Overall, the space complexity is O(n+m).