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