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
# Python3 code to demonstrate working of # Exceptional Split in String # Using loop + split() # initializing string test_str = "gfg, is, (best, for), Lazyroar" # printing original string print ( "The original string is : " + test_str) # Exceptional Split in String # Using loop + split() 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) # printing result print ( "The string after exceptional split : " + str (res)) |
The original string is : gfg, is, (best, for), Lazyroar The string after exceptional split : ['gfg', ' is', ' (best, for)', ' Lazyroar']
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
# Python3 code to demonstrate working of # Exceptional Split in String # Using regex() import re # initializing string test_str = "gfg, is, (best, for), Lazyroar" # printing original string print ( "The original string is : " + test_str) # Exceptional Split in String # Using regex() res = re.split(r ', (?!\S\)|\()' , test_str) # printing result print ( "The string after exceptional split : " + str (res)) |
The original string is : gfg, is, (best, for), Lazyroar The string after exceptional split : ['gfg', ' is', ' (best, for)', ' Lazyroar']
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
# Python3 code to demonstrate working of # Exceptional Split in String # Using str.split() and list comprehension # initializing string test_str = "gfg, is, (best, for), Lazyroar" # printing original string print ( "The original string is : " + test_str) # Exceptional Split in String # Using str.split() and list comprehension 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]) # printing result print ( "The string after exceptional split : " + str (res_list)) |
The original string is : gfg, is, (best, for), Lazyroar The string after exceptional split : ['gfg', 'is', '(best', 'for)', 'Lazyroar']
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.