Python | Exceptional Split in String
Last Updated :
08 Apr, 2023
Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should not be enclosed by brackets. Lets discuss certain ways in which this task can be performed.
Method #1: Using loop + strip() This is brute force way in which we perform this task. In this we construct each element of list as words in String accounting for brackets and comma to perform split.
Python3
test_str = "gfg, is, (best, for), geeks"
print ( "The original string is : " + test_str)
temp = ''
res = []
check = 0
for ele in test_str:
if ele = = '(' :
check + = 1
elif ele = = ')' :
check - = 1
if ele = = ', ' and check = = 0 :
if temp.strip():
res.append(temp)
temp = ''
else :
temp + = ele
if temp.strip():
res.append(temp)
print ( "The string after exceptional split : " + str (res))
|
Output :
The original string is : gfg, is, (best, for), geeks
The string after exceptional split : ['gfg', ' is', ' (best, for)', ' geeks']
Method #2: Using regex() This is yet another way in which this task can be performed. In this, we use a regex instead of manual brute force logic for brackets comma and omit that from getting split.
Python3
import re
test_str = "gfg, is, (best, for), geeks"
print ( "The original string is : " + test_str)
res = re.split(r ', (?!\S\)|\()' , test_str)
print ( "The string after exceptional split : " + str (res))
|
Output :
The original string is : gfg, is, (best, for), geeks
The string after exceptional split : ['gfg', ' is', ' (best, for)', ' geeks']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using str.split() and list comprehension
- Initialize a list patterns with the patterns that we want to exclude from the split. In this case, the patterns are ‘, ‘ and ‘, ‘(.
- Use the split() method of the string object to split the string into a list of substrings based on the delimiter ‘, ‘. Store the resulting list in a variable split_str.
- Use a list comprehension to iterate over each substring in split_str and check if it matches any pattern in patterns. If it does, join it with the next substring in split_str. If it doesn’t, add it to a new list res_list.
- Return the res_list as the result.
Python3
test_str = "gfg, is, (best, for), geeks"
print ( "The original string is : " + test_str)
patterns = [ ', ' , ', (' ]
split_str = test_str.split( ', ' )
res_list = [split_str[ 0 ]]
for i in range ( 1 , len (split_str)):
if any (split_str[i - 1 ].endswith(p) for p in patterns):
res_list[ - 1 ] + = ', ' + split_str[i]
else :
res_list.append(split_str[i])
print ( "The string after exceptional split : " + str (res_list))
|
Output
The original string is : gfg, is, (best, for), geeks
The string after exceptional split : ['gfg', 'is', '(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.
Please Login to comment...