69,379
社区成员
发帖
与我相关
我的任务
分享
匹配模式1:char *regex="(char)"; //无匹配数量词,最大限度匹配
匹配模式2:char *regex="(char).*(char)"; //.*任意数量匹配,匹配限度可变
#include<stdio.h>
#include<regex.h>
#define nmatch 5
char *string="Tabs are 8 characters, and thus characters.\n";
int main()
{
regex_t preg;
char *regex="(char).+(char)"; //匹配2个char
regmatch_t pmatch[nmatch];
unsigned int i,len;
bzero(&preg,sizeof(regex_t));
if(regcomp(&preg,regex,REG_EXTENDED)==0)
{
if(regexec(&preg,string,nmatch,pmatch,0)==0)
{
for(i=1;i<nmatch;i++) //i=0是全部结果,i=1开始是子匹配的值
{
if(pmatch[i].rm_so!=-1)
printf("%s\n",string+pmatch[i].rm_so);
}
}
}
return 0;
}