Open In App

Python | Extract substrings between brackets

Sometimes, while working with Python strings, we can have a problem in which we need to extract the substrings between certain characters that can be bracketed. This can have application in cases we have tuples embedded in string.  Let’s discuss certain ways in which this task can be performed.

Method #1: Using regex 



One way to solve this problem is by using regex. In this we employ suitable regex and perform the task of extraction of required elements.  




# Python3 code to demonstrate working of
# Extract substrings between brackets
# Using regex
import re
 
# initializing string
test_str = "geeks(for)geeks is (best)"
 
# printing original string
print("The original string is : " + test_str)
 
# Extract substrings between brackets
# Using regex
res = re.findall(r'\(.*?\)', test_str)
 
# printing result
print("The element between brackets : " + str(res))

Output : 

The original string is : geeks(for)geeks is (best)
The element between brackets : ['(for)', '(best)']

 

Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(m), where m is the number of matches of the pattern r’\(.*?\)’ in the input string test_str. The reason is that the function re.findall() creates a list res of all matches, so the space needed is proportional to the number of matches.

Method #2 : Using list comprehension + isinstance() + eval() 

The combination of the above methods can also be used to solve this problem. In this eval() assume the brackets to be tuples and helps the extraction of strings within them. 




# Python3 code to demonstrate working of
# Extract substrings between brackets
# Using list comprehension + eval() + isinstance()
 
# initializing string
test_str = "[(234, ), 4, (432, )]"
 
# printing original string
print("The original string is : " + test_str)
 
# Extract substrings between brackets
# Using list comprehension + eval() + isinstance()
res = [str(idx) for idx in eval(test_str) if isinstance(idx, tuple)]
 
# printing result
print("The element between brackets : " + str(res))

Output : 
The original string is : [(234, ), 4, (432, )]
The element between brackets : ['(234, )', '(432, )']

 

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using replace(),split(),startswith(),endswith() methods




# Python3 code to demonstrate working of
# Extract substrings between brackets
 
# initializing string
test_str = "[(234, ), 4, (432, )]"
 
# printing original string
print("The original string is : " + test_str)
 
# Extract substrings between brackets
test_str=test_str.replace("(","*(")
test_str=test_str.replace(")",")*")
x=test_str.split("*")
res=[]
for i in x:
    if i.startswith("(") and i.endswith(")"):
        res.append(i)
# printing result
print("The element between brackets : " + str(res))

Output
The original string is : [(234, ), 4, (432, )]
The element between brackets : ['(234, )', '(432, )']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using a loop and conditional statements

Step-by-step approach:




test_str = "geeks(for)geeks is (best)"
print("The original string is : " + test_str)
 
substrings = []
in_brackets = False
current_substring = ""
 
for c in test_str:
    if c == "(":
        in_brackets = True
    elif c == ")" and in_brackets:
        substrings.append(current_substring)
        current_substring = ""
        in_brackets = False
    elif in_brackets:
        current_substring += c
 
if current_substring:
    substrings.append(current_substring)
 
print("The element between brackets : " + str(substrings))

Output
The original string is : geeks(for)geeks is (best)
The element between brackets : ['for', 'best']

Time complexity: O(n), where n is the length of test_str.
Auxiliary space: O(m), where m is the number of substrings between brackets in test_str.

Method #6: Using split() and join() methods

Step-by-step approach:

Below is the implementation of the above approach:




test_str = "geeks(for)geeks is (best)"
print("The original string is : " + test_str)
 
substrings = []
split_str = test_str.split("(")
 
for s in split_str[1:]:
    split_s = s.split(")")
    if len(split_s) > 1:
        substrings.append(split_s[0])
 
substrings_str = ", ".join(substrings)
print("The element between brackets : " + substrings_str)

Output
The original string is : geeks(for)geeks is (best)
The element between brackets : for, best

Time Complexity: The time complexity of this method is O(n), where n is the length of the input string, as the split() and join() methods perform a single pass through the string.
Auxiliary Space: The auxiliary space complexity of this method is O(k), where k is the number of substrings between parentheses, as the method stores a list of these substrings.

Method #7: Using str.find() method and slicing

Step-by-step approach:

  1. Find the index of the first opening bracket “(” in the given string using the str.find() method.
  2. Find the index of the first closing bracket “)” in the given string using the str.find() method starting from the index found in step 1.
  3. Slice the substring between the two indices found in steps 1 and 2 using string slicing.
  4. Repeat steps 1-3 for all occurrences of the brackets in the string using a while loop.
  5. Join the list of substrings obtained in step 4 using the join() method and the desired separator.

Below is the implementation of the above approach:




test_str = "geeks(for)geeks is (best)"
print("The original string is : " + test_str)
 
substrings = []
start_index = test_str.find("(")
 
while start_index != -1:
    end_index = test_str.find(")", start_index+1)
    if end_index != -1:
        substrings.append(test_str[start_index+1:end_index])
    start_index = test_str.find("(", start_index+1)
 
substrings_str = ", ".join(substrings)
print("The element between brackets : " + substrings_str)

Output
The original string is : geeks(for)geeks is (best)
The element between brackets : for, best

Time complexity: O(n^2) (worst case when all characters are brackets)
Auxiliary space: O(n) (for storing the list of substrings)


Article Tags :