import re


def permutation_length(nonterminal):
    """
    :param nonterminal: a non-terminal of the form: '[P1234*2_1]
    :return: the length of the permutations within the non-terminal
    """

    NT_PERMUTATION = re.compile(r'P([0-9]+)')
    matches = NT_PERMUTATION.search(nonterminal)
    if matches is not None:
        assert matches is not None, 'bad format %s' % nonterminal
        permutation = matches.group(1)
        return len(permutation)
    else:
        return 0