    #include <iostream>
    #include <string>
     
    #include <regex.h>
     
    const int MAX_MATCHES(20);
     
    void elements(const std::string &input)
    {
        regex_t ElementRegEx;
        regcomp(&ElementRegEx, "([0-9]+)", REG_EXTENDED);
        
        std::string Elements(input);
        
        regmatch_t ElementMatches[MAX_MATCHES];
        while (!regexec(&ElementRegEx, Elements.c_str(), MAX_MATCHES, ElementMatches, 0))
        {
            const regmatch_t &Match = ElementMatches[0];
            
            if ((Match.rm_so != -1) && (Match.rm_eo != -1))
            {
                const std::string CurrentElement(Elements.substr(Match.rm_so, Match.rm_eo - Match.rm_so));
                std::cout << '\t' << CurrentElement << '\n';
                
                if (Elements.size() > static_cast<size_t>(Match.rm_eo))
                Elements = Elements.substr(Match.rm_eo);
                else
                Elements = "";
            }
        }
    }
     
    void cycles(const std::string &input)
    {
        regex_t CycleRegEx;
        regcomp(&CycleRegEx, "([0-9]+[ ]*,[ ]*)*[0-9]+", REG_EXTENDED);
        
        std::string Cycles(input);
        
        regmatch_t CycleMatches[MAX_MATCHES];
        while (!regexec(&CycleRegEx, Cycles.c_str(), MAX_MATCHES, CycleMatches, 0))
        {
            const regmatch_t &Match = CycleMatches[0];
            
            if ((Match.rm_so != -1) && (Match.rm_eo != -1))
            {
                const std::string CurrentCycle(Cycles.substr(Match.rm_so, Match.rm_eo - Match.rm_so));
                std::cout << CurrentCycle << '\n';
                elements(CurrentCycle);
                
                if (Cycles.size() > static_cast<size_t>(Match.rm_eo))
                Cycles = Cycles.substr(Match.rm_eo + 1);
                else
                Cycles = "";
            }
        }
    
        regfree(&CycleRegEx);
    }
     
    int main(int argc, char **argv)
    {
        std::string input("(1,2,5)(3,4)");
        
        std::cout << "input: " << input << "\n\n";
        
        cycles(input);
        return 0;
    }