日期:2014-05-16 浏览次数:20895 次
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<regex.h> /* 取子串的函数 */ void substr (char *dest,char *str, unsigned start, unsigned end) { unsigned n = end - start; printf("\n%u \n",n); strncpy(dest, str + start, n); dest[n] = 0; } int Pattern(char *str,char *pattern,char *result) { int cflags=REG_EXTENDED,z; regex_t reg; regmatch_t pm[10]; const size_t nmatch=10; z = regcomp (®, pattern, cflags); printf("%d\n",z); if(z!=0) return z; z = regexec (®, str, nmatch, pm, 0); printf("%d\n",z); if(z!=0) return z; substr(result,str, pm[0].rm_so, pm[0].rm_eo); return z; } int main(int argc, char **argv) { char result[100]; Pattern("Windows 95","Windows (?=95|98|NT|2000)",result); printf("%s\n",result); return 0; }
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<regex.h> /* 取子串的函数 */ void substr (char *dest,char *str, unsigned start, unsigned end) { unsigned n = end - start; printf("[%u, %u)=%u\n", start, end, n); strncpy(dest, str + start, n); dest[n] = 0; } int Pattern(char *str,char *pattern,char *result) { int cflags=REG_EXTENDED, z; regex_t reg; regmatch_t pm[10]; const size_t nmatch=10; char errmsg[100] = {0}; z = regcomp (®, pattern, cflags); printf("1. z=%d\n",z); if(z!=0) { regerror(z, ®, errmsg, 100); printf("1. errmsg=[%s]\n", errmsg); return z; } z = regexec (®, str, nmatch, pm, 0); printf("2. z=%d\n",z); if(z!=0) { regerror(z, ®, errmsg, 100); printf("2. errmsg=[%s]\n", errmsg); return z; } substr(result, str, pm[0].rm_so, pm[0].rm_eo); return z; } int main(int argc, char **argv) { char result[100] = {0}; char pat[] = "Windows (?=95|98|NT|2000)"; int ret = Pattern("Windows 95", pat, result); printf("ret=%d, result=[%s]\n", ret, result); return 0; }