regexec(3)'s usage:Match Results with Subexpressions
http://www.gnu.org/software/libc/manual/html_node/Regexp-Subexpressions.html#Regexp-Subexpressions An example program: #include <stdio.h> #include <sys/types.h> #include <regex.h> /* 取子串的函数 */ static char* substr(const char*str, unsigned start, unsigned end) { unsigned n = end - start; static char stbuf[256]; strncpy(stbuf, str + start, n); stbuf[n] = 0; return stbuf; } /* 主程序 */ int main(int argc, char** argv) { int x, z, lno = 0, cflags = REG_EXTENDED; char ebuf[128], lbuf[256]; regex_t reg; regmatch_t pm[10]; const size_t nmatch = 10; /* 编译正则表达式*/ z = regcomp(®, "(reg|regular)[ ]?(ex|expression)", cflags); /* 逐行处理输入的数据 */ while(fgets(lbuf, sizeof(lbuf), stdin)) { ++lno; if ((z = strlen(lbuf)) > 0 && lbuf[z-1] == '\n')