1

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 ?

martin_0004
  • 357
  • 1
  • 2
  • 15
  • 1
    Do you really need two expressions, or do you need just one that matches "curly brackets which do not contain any other curly brackets"? – Bryan Oakley Feb 11 '16 at 17:31
  • 1
    Also, is it a requirement that you use regular expressions, or are other solutions valid? You can pretty easily iterate over the characters and count curly braces. – Bryan Oakley Feb 11 '16 at 17:44

2 Answers2

0

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

vks
  • 67,027
  • 10
  • 91
  • 124
  • Your first one fails is you have two sets of braces in the same outer brace. eg `{aaa{bbb}ccc{ddd}eee}` will return `{bbb}` – Holloway Feb 11 '16 at 17:29
0

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 *

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Well actually, what I am looking for are: Case 1) curly brackets which do not contain other brackets AND which are themselves not within curly brackets or parentheses; Case 2) curly brackets which do not contain other brackets AND which themselves are within curly brackets or parentheses. :-) – martin_0004 Feb 11 '16 at 17:31
  • @phodor: it's not clear if you need two expressions, or one expression that returns the combination of the two cases. Can you please clarify in the question whether you specifically need two separate expressions? – Bryan Oakley Feb 11 '16 at 17:32
  • @phodor: it's not any more clear. You state you need to find all matches for "curly brackets which do not contain any other curly brackets.". You then say you've tried two separate expressions but you haven't stated whether you _require_ two separate expressions. Is there a reason why one expression won't work? – Bryan Oakley Feb 11 '16 at 17:40