1 #include<stdio.h> 2 #include<sys/types.h> 3 #include<regex.h> 4 #include<memory.h> 5 #include<stdlib.h> 6 7 int main(){ 8 9 char *bematch = "hhhericchd@gmail.com"; 10 char *pattern = "h{3,10}(.*)@.{5}.(.*)"; 11 char errbuf[1024]; 12 char match[100]; 13 regex_t reg; 14 int err,nm = 10; 15 regmatch_t pmatch[nm]; 16 17 if(regcomp(®,pattern,REG_EXTENDED) < 0){ 18 regerror(err,®,errbuf,sizeof(errbuf)); 19 printf("err:%s\n",errbuf); 20 } 21 22 err = regexec(®,bematch,nm,pmatch,0); 23 24 if(err == REG_NOMATCH){ 25 printf("no match\n"); 26 exit(-1); 27 }else if(err){ 28 regerror(err,®,errbuf,sizeof(errbuf)); 29 printf("err:%s\n",errbuf); 30 exit(-1); 31 } 32 33 for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++){ 34 int len = pmatch[i].rm_eo-pmatch[i].rm_so; 35 if(len){ 36 memset(match,'\0',sizeof(match)); 37 memcpy(match,bematch+pmatch[i].rm_so,len); 38 printf("%s\n",match); 39 } 40 } 41 return 0; 42 }
|