Python: How to match nested parentheses with regex? -


I am trying to match string like a mathematical expression, which is a nested bracket. import re p = re.compile ('\ (. +)') Str = '(((1 + 0) +1) +1) Print printing (files)

<'(' ((+ 1 + 1) +1) +1) ']

I wanted to match it to all the attached expressions , Such as (1 +0), ((1 + 0) + 1) ... I do not care if it matches the unwanted ones (((+ 1 + 0), I can take care of those people.

Why is not it already doing this, and how can I do it?

Try regular expressions It is possible to match as much text as possible to which all your strings are consumed. It does not find matches in addition to regular expression on parts of that string, that is why you get only one answer .

If you are actually trying to parse math expressions, then use a real analysis parsing. If you really want to capture only May include the pieces within the bracket, when you see (and) fall just loop characters of calculating a counter increments

Comments