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
# 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)) |
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
# 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)) |
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
# 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)) |
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:
- Initialize an empty list called substrings.
- Initialize a boolean variable called in_brackets to False.
- Initialize an empty string called current_substring.
- Loop through each character in the string test_str.
- If the current character is an opening bracket (set in_brackets to True.
- If the current character is a closing bracket ) and in_brackets is True, append current_substring to substrings and set current_substring to an empty string. Set in_brackets to False.
- If in_brackets is True, add the current character to current_substring.
- After the loop is finished, if current_substring is not empty, append it to substrings.
- Print substrings.
Python3
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)) |
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.
Please Login to comment...