教程: https://blog.csdn.net/ljp1919/article/details/47753559
尝试:
#include<stdio.h> #include<regex.h> #include<string.h> int main(int argn, char **argv) { char *pattern = "[0-9]*qq.com"; char *string = argv[1]; char buf[100]; int ret = 0; int i = 0; // 存放匹配后的信息 const size_t nmatch = 5; regmatch_t pm[5]; // 存放编译后的信息 regex_t reg; // 编译 ret = regcomp(®, pattern, REG_EXTENDED); if (ret != 0) { printf("编译错误\n"); return -1; } // 匹配 ret = regexec(®, string, nmatch, pm, 0); if (ret == REG_NOMATCH) { printf("未匹配到结果\n"); return 0; } else if (ret != 0) { printf("匹配错误\n"); return -1; } // 输出匹配结果 for (i=0;i<nmatch&&pm[i].rm_so!=-1;i++) { strncpy(buf, string+pm[i].rm_so, pm[i].rm_eo-pm[i].rm_so); printf("%d: %s\n", i, buf); } // 释放 regfree(®); return 0; }