代码如下:
#include
#include
#include
#include
#define OK 0
#define FALSE -1
int regular(const char* pattern, const char* str, char* res)
{
int z;
regex_t reg;
regmatch_t pm;
char ebuf[128];
z = regcomp(®, pattern, 0);
if (0 != z)
{
return FALSE;
}
z = regexec(®, str, 1, &pm, 0);
if (0 != z)
{
regerror(z, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern);
return FALSE;
}
unsigned start = pm.rm_so;
unsigned end = pm.rm_eo;
strncpy(res, str+start, end-start);
regfree(®);
return OK;
}
int main(int argc, char **argv)
{
char result[64] = {0};
if ( OK == regular("\\[.*\\]", "This is a test program, [google] is my love!!", result) )
{
printf("OK\n");
printf("%s\n", result);
}
else
{
printf("FALSE\n");
}
return 0;
}
程序输出如下:
[root@localhost program]# ./a.out
OK
[google]
阅读(3126) | 评论(0) | 转发(0) |