免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2285 | 回复: 0
打印 上一主题 下一主题

正则库pcre使用例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-18 20:54 |只看该作者 |倒序浏览
例子1:

#include stdio.h>
#include string.h>
#include pcre.h>
/**********************************************************************
*#include  *
*parameters: src: string *
* pattern: regular expression *
*return: match >= 0 and nomatch
int fun_ismatch( char* src, char* pattern)
{
        pcre *re;
        const char *error;
        int erroffset;
        int rc;
        if( (re = pcre_compile( pattern, 0, &error, &erroffset, NULL)) == NULL) goto bad;
        if( (rc = pcre_exec( re, NULL, src, strlen(src), 0, 0, NULL, 0))  0) goto bad;
        free(re);
        return rc;
bad:
        free(re);
        return -1;
}
int main (int argc, char **argv)
{
        struct timeval tpstart,tpend;
        const char *pattern = "^http://.+?btchina.net/download.php*";
        argv[1] = "http://dl1.www2.btchina.net/download.php?s=1125f72b0f6f6887&attachmentid=1064643@http://bt3.btchina.net/";
        gettimeofday( &tpstart, NULL);
        int result = fun_ismatch( argv[1], (char*)pattern);
        gettimeofday( &tpend, NULL);
        printf ("result: %d\nuse time: %u\n", result, tpend.tv_usec - tpstart.tv_usec);
        return;
}

例子2:

#include stdio.h>
#include string.h>
#include pcre.h>
               
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
        
int main()
{
        pcre *re;
        const char *error;
        int erroffset;
        int ovector[OVECCOUNT];
        int rc, i;
        
        char src [] = "111 Hello World 222";
        char pattern [] = "(.*)";
               
        printf("String : %s\n", src);
        printf("Pattern: \"%s\"\n", pattern);
        
        re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
        if (re == NULL) {
                printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
                return 1;
        }
        rc = pcre_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
        if (rc  0) {
                if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");
                else printf("Matching error %d\n", rc);
                free(re);
                return 1;
        }
        printf("\nOK, has matched ...\n\n");
        for (i = 0; i  rc; i++) {
                char *substring_start = src + ovector[2*i];
                int substring_length = ovector[2*i+1] - ovector[2*i];
                printf("%2d: %.*s\n", i, substring_length, substring_start);
        }
        free(re);
        return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/91335/showart_1903649.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP