it's actually not possible to do this by using regular expressions, because regular expressions express a language defined by a regular grammar that can be solved by a non finite deterministic automaton, where matching is represented by states ; then to match nested parenthesis, you'd need to be able to match an infinite number of parenthesis and then have an automaton with an infinite number of states.
To be able to cope with that, we use what's called a push-down automaton, that is used to define the context free grammar.
So if your regex does not match nested parenthesis, it's because it's expressing the following automaton and does not match anything on your input:
Play with it
As a reference, please have a look at MIT's courses on the topic:
So one of the ways to parse your string efficiently, is to build a grammar for nested parenthesis (pip install pyparsing
first):
>>> import pyparsing
>>> strings = pyparsing.Word(pyparsing.alphanums)
>>> parens = pyparsing.nestedExpr( '(', ')', content=strings)
>>> parens.parseString('(NP (NNP Hoi) (NN Hallo) (NN Hey) (NNP (NN Ciao) (NN Adios)))').asList()
[['NP', ['NNP', 'Hoi'], ['NN', 'Hallo'], ['NN', 'Hey'], ['NNP', ['NN', 'Ciao'], ['NN', 'Adios']]]]
N.B.: there exists a few regular expressions engines that do implement nested parenthesis matching using the push down. The default python re
engine is not one of them, but an alternative engine exists, called regex
(pip install regex
) that can do recursive matching (which makes the re engine context free), cf this code snippet:
>>> import regex
>>> res = regex.search(r'(?<rec>\((?:[^()]++|(?&rec))*\))', '(NP (NNP Hoi) (NN Hallo) (NN Hey) (NNP (NN Ciao) (NN Adios)))')
>>> res.captures('rec')
['(NNP Hoi)', '(NN Hallo)', '(NN Hey)', '(NN Ciao)', '(NN Adios)', '(NNP (NN Ciao) (NN Adios))', '(NP (NNP Hoi) (NN Hallo) (NN Hey) (NNP (NN Ciao) (NN Adios)))']