regmatch_t
のセットを渡す必要があります。正規表現では、マッチのインデックスを埋め込むことができます。 1つのコマンドライン引数(テストする文字列)で以下のプログラムを試してみてください。
マッチのインデックスを取得したら、後で引き出すのはかなり簡単です。 (注:matches[0]
は式全体の一致になるので、部分式はmatches[1]
から始まる)
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
int main(int argc, char* argv[])
{
const char* pattern = "{{()*(([[:alnum:]]+\\.)*)?[[:alnum:]]+()*}}";
regex_t rex;
int rc;
if ((rc = regcomp(&rex, pattern, REG_EXTENDED))) {
fprintf(stderr, "error %d compiling regex\n", rc);
/* retrieve error here with regerror */
return -1;
}
regmatch_t* matches = malloc(sizeof(regex_t) * (rex.re_nsub + 1));
if ((rc = regexec(&rex, argv[1], rex.re_nsub + 1, matches, 0))){
printf("no match\n");
/* error or no match */
} else {
for(int i = 0; i < rex.re_nsub; ++i) {
printf("match %d from index %d to %d: ", i, matches[i].rm_so,
matches[i].rm_eo);
for(int j = matches[i].rm_so; j < matches[i].rm_eo; ++j) {
printf("%c", argv[1][j]);
}
printf("\n");
}
}
free(matches);
regfree(&rex);
return 0;
}