Home > other >  RegEx removing pair parantheses only
RegEx removing pair parantheses only

Time:07-31

How to replace only pair parentheses by nothing in this expression? I tried many ways, but then I decided to post my question here...

Code:

expression = ')([()])('
pattern = r'[(\(.*\)]'
nothing = ''
print(re.sub(pattern, nothing, expression))  # Expected to be ')[]('

Another expressions to validating:

// True
<s>HTML is a programming language</s>
(1 2) * (3 4) / 4!
{1, 2, 3, ..., 10}
([{<>}])

// False
<}>
)[}({>]<
<[{}]><

As you guess, I want to solve a classic problem in new way... Not only parentheses, another punctuation marks such as brackets, angle brackets, and braces should be removed. (Use re.sub(r'[^\(\)\[\]\{\}\<\>]', '', expr) to clean them)

I want to drop them in one step, but all answers are accepted...

CodePudding user response:

Based on How to remove all text between the outer parentheses in a string?:

import re
def rem_parens(text):
    n = 1  # run at least once
    while n:
        text, n = re.subn(r'\(([^()]*)\)', r'\1', text)
    return text

print(rem_parens(")([()])("))

Results: )[](

See Python proof

How to extend to accept more bracket types

Add alternatives to the expression and backreferences to the replace:

re.subn(r'\(([^()]*)\)|\[([^][]*)]|<([^<>]*)>|\{([^{}]*)}', r'\1\2\3\4', text)
  • Related