I'm trying to remove the characters between the parentheses and brackets based on the length of characters inside the parentheses and brackets.
Using this:
def remove_text_inside_brackets(text, brackets="()[]"):
count = [0] * (len(brackets) // 2) # count open/close brackets
saved_chars = []
for character in text:
for i, b in enumerate(brackets):
if character == b: # found bracket
kind, is_close = divmod(i, 2)
count[kind] += (-1)**is_close # `+1`: open, `-1`: close
if count[kind] < 0: # unbalanced bracket
count[kind] = 0 # keep it
else: # found bracket to remove
break
else: # character is not a [balanced] bracket
if not any(count): # outside brackets
saved_chars.append(character)
return ''.join(saved_chars)
I'm able to remove the characters between the parentheses and brackets, but I cannot figure out how to remove the characters based on the length of characters inside.
I wanted to remove characters between the parentheses and brackets if the length <=4
with parentheses and brackets if they are >4
remove only parentheses and brackets.
Sample Text:
text = "This is a sentence. (RMVE) (Once a day) [twice a day] [RMV]"
Output:
print(remove_text_inside_brackets(text))
This is a sentence.
Desired Output:
This is a sentence. Once a day twice a day