1 #include <regex.h> 2 #include <iostream> 3 #include <sys/types.h> 4 #include <stdio.h> 5 #include <cstring> 6 using namespace std; 7 int main() 8 { 9 char pattern[20]="(.*)[0-9]{8}(.*)"; 10 char buffer[]="run_log_201601011.txt"; 11 const size_t nmatch = 1; 12 regmatch_t pm[nmatch]; 13 regex_t reg; 14 int z,i; 15 16 regcomp(®,pattern,REG_EXTENDED); 17 18 z = regexec(®,buffer,nmatch,pm,REG_NOTBOL); 19 20 if(z == REG_NOMATCH) 21 { 22 printf("match faild!\r\n"); 23 }else{ 24 printf("match ok\r\n"); 25 26 for(i=pm[0].rm_so;i<pm[0].rm_eo;++i) 27 {printf("%c",buffer[i]);} 28 printf("\r\n"); 29 } 30 31 regfree(®); 32 return 0; 33 }