import re


def read_ndxfile(filename) :
    """
    Parse a Gromacs ndx file

    Parameters
    ----------
    filename : string
      the name of the file to parse

    Returns
    -------
    dictionary of lists
      the parsed groups
    """

    ndxstr = ""
    with open(filename, "r") as f :
        ndxstr = "".join(f.readlines())

    ndxsplit = re.split("(\[.+\])", ndxstr)
    ndxdict = {}
    for namestr, indexstr in zip(ndxsplit[1::2], ndxsplit[2::2]):
        ndxdict[namestr[1:-1].strip()] =  \
            map(int, re.sub("\s+"," ",indexstr).strip().split())
    return ndxdict