实现
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
int my_match(char* pattern,char* buf){
int status,i;
int flag=REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch=1;
regex_t reg;
regcomp(®,pattern,flag);
status=regexec(®,buf,nmatch,pmatch,0);
for(i=pmatch[0].rm_so;i<pmatch[0].rm_eo;++i){
putchar(buf[i]);
}
printf("\n");
regfree(®);
return status;
}
使用
int my_reg(){
char pattern[1024]= "e(.*)o";
char buf[1024]="Hello World";
int status=my_match(pattern,buf);
if(status==REG_NOMATCH)
printf("No match!\n");
else if(0 == status){
printf("匹配成功!\n");
}
return 0;
}