登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

满天星

鬓 毛 八 九 已 成 霜 , 此 际 逢 春 只 自 伤

 
 
 

日志

 
 

正则库pcre使用例子   

2009-07-17 14:51:08|  分类: Program |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

例子1:
 

DE<#include <stdio.h>
#include <string.h>
#include <pcre.h>

/**********************************************************************
*#include <pcre.h> *
*parameters: src: string *
* pattern: regular expression *
*return: match >= 0 and nomatch < 0 *
*date: 2008-03-12 *
*developer: Leon *
**********************************************************************/

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;
}
DE<

 

例子2:

 

DE<#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 <title>Hello World</title> 222";
        char pattern [] = "<title>(.*)</title>";
                
        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;
}
DE<

 

[From:] http://blog.chinaunix.net/u3/91335/showart_1903649.html

  评论这张
 
阅读(1714)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018