2

Let's say I have a string in which I want to remove text between parentheses (and the parentheses themselves). For example:

input:  'Lorem ipsum (dolor) sit amet'
output: 'Lorem ipsum  sit amet'

In general, I would like to remove text between any given couple of opening and closing symbols. For example, if my list of symbols is ['()', '[]', '<>'] then

input:  '(Lorem) <ipsum> dolor [sit amet]'
output: '  dolor '

This answer is of course very related, and simple enough. However, it does have a huge problem: it does not work in cases in which different types of parentheses are used together within the string. For example, the linked answer fails in this case:

input:  '(Lorem ipsum] dolor) sit amet'
output: ' sit amet'

Indeed, that solution provides the output ' dolor) sit amet'.

Does anybody know if this problem can be solved with a regex?


Some more details. In my specific case, there can't be overlapping matches. For example, this case is not possible:

'(Lorem [ipsum) dolor] sit amet'

Also, there can't be nested matches. So these cases are not possible:

'(Lorem ipsum (dolor sit)) amet'
'(Lorem [ipsum] dolor) sit amet'
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50

0 Answers0