Python 3.4 - Regular Expressions - Matching Innermost curly brackets within & not within other brackets

271 Views Asked by At

In Python 3.4, I am trying to use two different regular expressions for matching two types of innermost curly brackets, i.e. curly brackets which do not contain any other curly brackets.

Regular Expression 1) Match innermost curly brackets which are not within other curly brackets or parentheses, i.e. the following expression

re.findall(r'...something...',"aaa{bbb{ccc}ddd}eee{fff}ggg(hhh{iii})jjj{kkk}") 

would return this

['{fff}','{kkk}']

At the moment I am trying with the following expression

re.findall(r'[^{(]*\{[^{}]+\}[^})]*',"aaa{bbb{ccc}ddd}eee{fff}ggg(hhh{iii})jjj{kkk}")

which unfortunately returns this:

['bbb{ccc}ddd', '}eee{fff}ggg(hhh{iii', '})jjj{kkk}']

Regular Expression 2) Match innermost curly brackets which are within other curly brackets or parentheses, i.e. the following expression

re.findall(r'...something...',"aaa{bbb{ccc}ddd}eee{fff}ggg(hhh{iii})jjj{kkk}") 

would return this

['{ccc}','{iii}'] 

I am trying with this expression:

 re.findall(r'[{(]*\{[^{}]+\}[})]*',"aaa{bbb{ccc}ddd}eee{fff}ggg(hhh{iii})jjj{kkk}")

but it returns this:

['{ccc}', '{fff}', '{iii})', '{kkk}']

Any idea on how to modify these regular expressions to have the correct matches ?

2

There are 2 best solutions below

1
On

For first case you can use

{[^{}]*}(?![^{}()]*(?:[({][^{}()]*[)}])*[^(){}]*[})])

See demo.

https://regex101.com/r/aT3kG2/3

For second case u can use

{[^{}]*}(?=[^{}()]*(?:[({][^{}()]*[)}])*[^(){}]*[})])

See demo.

https://regex101.com/r/aT3kG2/4

3
On

If you want a single regular expression that matches "curly brackets which do not contain any other curly brackets", search for exactly that -- curly brackets which do not contain other curly brackets.

For example:

re.findall(r'{[^{}]+}',"aaa{bbb{ccc}ddd}eee{fff}ggg(hhh{iii})jjj{kkk}") 

This returns the combination of both of your cases:

['{ccc}', '{fff}', '{iii}', '{kkk}']

If you also want to find empty brackets (eg: {}), change the + to a *