-1

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...

  • 3
    Does this answer your question? [Regular expression to match balanced parentheses](https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses) – CollinD Jul 30 '22 at 02:34
  • Use a lazy dot in your pattern: `pattern = r'[(\(.*?\)]'` – Tim Biegeleisen Jul 30 '22 at 02:37
  • @TimBiegeleisen It doesn't work when I tried it on a few expressions... It selects the pairs but it might not be in front of it but behind it! – Artin Mohammadi Jul 30 '22 at 02:42
  • @CollinD I have seen them before, but none of them satisfied me! People there mostly try to solve the problem recursively, which is not very effective at all (if it works at all)... – Artin Mohammadi Jul 30 '22 at 02:45
  • 7
    If you want to handle nested parentheses, then **do not use a regex**. You need a parser. – mozway Jul 30 '22 at 02:57
  • @mozway I should have done the same thing until now, but I wanted to solve a classic challenge in a new way, that doesn't sound like a good idea in regular expressions... **Also stacks are not interesting either, but it is better than making a parser!** – Artin Mohammadi Jul 30 '22 at 03:10
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jul 30 '22 at 03:12
  • 2
    Expression parsing is a 60-year-old problem. If there were better solutions, they would have been discovered in the 20th Century. – Tim Roberts Jul 30 '22 at 03:33
  • 2
    Regexes originate from a syntax used to express *regular languages*, a category of very simple formal languages that does not include constructs like balanced parentheses. While common regex dialects extend the original syntax in ways that go beyond regular languages, the core is still deliberately un-expressive, and most dialects cannot express what you're looking for. – user2357112 Jul 30 '22 at 03:43

1 Answers1

1

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)
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37