我想提取字符串的一部分并包含在数组中。这是我目前的代码:
int r;
regex_t reg;
regmatch_t match[2];
char *line = "[ Page 1 ] [ Page 2asdasdasd asdas ] [ asdasdas asdas ]";
regcomp(®, "[[](.*?)[]]", REG_ICASE | REG_EXTENDED);
/* ^------^ capture page number */
r = regexec(®, line, 2, match, 0);
if (r == 0) {
printf("Match!\n");
printf("0: [%.*s]\n", match[0].rm_eo - match[0].rm_so, line + match[0].rm_so);
printf("1: %.*s\n", match[1].rm_eo - match[1].rm_so, line + match[1].rm_so);
} else {
printf("NO match!\n");
}
这将导致:
1: Page 1 ] [ Page 2asdasdasd asdas ] [ asdasdas asdas
我需要得到这样的东西:
array("Page 1", "Page 2asdasdasd asdas", "asdasdas asdas");
有可能吗?