#include <regex.h>
#include <stdio.h>

int main( int argv, char* args[] )
{
    char checkString[] = "abc, def, ghi";                   // チェックをする文字列
    const char regex[] = "([a-z]+), ([a-z]+), ([a-z]+)";    // マッチングをする文字列
    regex_t regexBuffer;    // 正規表現オブジェクト
    int i,j;
    int size;
    // パターンにマッチングしたインデックス格納する構造体
    regmatch_t patternMatch[4];

    // 正規表現オブジェクトのコンパイル
    if( regcomp( &regexBuffer, regex, REG_EXTENDED | REG_NEWLINE ) != 0 )
    {
        puts("regex compile failed" );
        return 1;
    }

    size = sizeof( patternMatch ) / sizeof( regmatch_t );
    if( regexec( &regexBuffer, checkString, size, patternMatch, 0 ) != 0 )
    {
        puts("No match!!");
        return 1;
    }

    // マッチした場合patternMatch構造体に文字列のindex番号が入る
    // 配列の数がマッチ数を超えていた場合超えた構造体の各要素には-1が入る
    for( i = 0; i < size; ++i )
    {
        int startIndex = patternMatch[i].rm_so;
        int endIndex = patternMatch[i].rm_eo;
        if( startIndex == -1 || endIndex == -1 )
        {
           	puts("exit");
            continue;
        }
      	for(j=startIndex;j<endIndex;j++)putchar(checkString[j]);
        putchar('\n');
    }

    // オブジェクトの開放
    regfree( &regexBuffer );
    return 0;
}