Isbister
Isbister

Reputation: 946

Python remove inner brackets and keep outer brackets

I am struggling with Regex, I have read the wiki and played around, but I cant seem to make the right match.

string_before = 'President [Trump] first name is [Donald], so his full name is [[Donald] [Trump]]' 
string_after = 'President [Trump] first name is [Donald], so his full name is [Donald Trump]' 

I want to remove any possible brackets inside the outer brackets while keeping the outer brackets and the text inside.

Could this be solved easy in python without regex?

Upvotes: 1

Views: 591

Answers (3)

tripleee
tripleee

Reputation: 189297

In the specific case of two adjacent bracketed expressions inside a pair of brackets, you can do

string = re.sub(r'\[\[([^][]+)\] \[([^][]+)\]\]', r'[\1 \2]', string)

This does not conveniently extend to an arbitrary number of adjacent bracketed expressions, but perhaps it's enough for your needs.

Upvotes: 1

Kent
Kent

Reputation: 195029

In [1]: import re
In [2]: before='blablabla [[Donald] [Trump]] blablabla'
In [3]: l=before.find('[')+1
In [4]: r=before.rfind(']')
In [5]: before[:l] + re.sub( r'[][]','',before[l:r]) + before[r:]
Out[5]: 'blablabla [Donald Trump] blablabla'

Just show one way to go, error checking/handling was omitted.

Upvotes: 0

Matt Fortier
Matt Fortier

Reputation: 1223

Regex will cause you more harm than good for such problems. You will need to write some parsing logic based on grammar or rules.

You could for example take a look at Finite-State Transducers (1, 2), which would be a suitable method of parsing nested constructions, but it's more complex than Regex to understand and use.

Upvotes: 1

Related Questions