Search code examples
pythonstringparsingparentheses

Python inner parenthetic string parsing


Alright, Currently, if given a string like such:

A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5,(F:0.6,G:0.7)H:0.8

I am using this:

child = Pstring[Pstring.find('(')+1:Pstring.find(')')]

To iterate through the string, and print out the inner parenthesis, and assign it to the variable 'child'

Now, my question is, how can I do the same for:

W:1.0,X:1.1(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5,(F:0.6,G:0.7)H:0.8)Y:0.9  

Which just simply contains an outside parenthesis to show that everything(except W and X) are children of Y

I currently get an output of 'child' as:

A:0.1,B:0.2,(C:0.3,D:0.4

Whereas what I want the code to do is to first parse through the outside parenthesis, and grab the inner ones first, then work on the outside last.

Thanks!


Solution

  • If you just want the contents of the inner parentheses, you can use re.findall() with the following regular expression:

    \(([^()]*)\)
    

    For example:

    >>> import re
    >>> s = 'W:1.0,X:1.1(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5,(F:0.6,G:0.7)H:0.8)Y:0.9'
    >>> re.findall(r'\(([^()]*)\)', s)
    ['C:0.3,D:0.4', 'F:0.6,G:0.7']
    

    Explanation:

    \(        # literal '('
    (         # start capturing group
      [^()]*    # any characters except '(' and ')', any number
    )         # end capturing group
    \)        # literal ')'
    

    re.findall() returns the contents of the capturing group for each match.