使用PCRE库多次匹配同一个字符串

#include <string.h>
#include <stdio.h>
#include <pcre.h>
 
intmain()
{
    pcre *re;
 
    constchar *error;
    interrorOffset, i = 0;
    /**
     * pcre_exec匹配的结果
     * ovector的结构为
     * {匹配结果1的起始位置,匹配结果1的结束位置,匹配结果2的起始位置,...匹配结果N的结束位置}
     */
    intoveccount = 2, ovector[oveccount];
     
    /**
     * rc是pcre_exec匹配到的结果数量
     */
    intrc;
    /**
     * pcre_exec执行的偏移量
     * 从匹配到的结果的结束位置开始下一次匹配
     */
    intexec_offset = 0;
     
 
    constchar *captured_string;
    char*subject = "1t2t3t4t5t6t7t8t9t0tatbtct黄t避孕t";
    char*pattern = "[^t]+t";
 
    re = pcre_compile( pattern, PCRE_CASELESS, &error, &errorOffset, NULL );
 
    if( re == NULL ) {
        printf("compilation failed at offset%d: %s\n", errorOffset, error);
        return0;
    }
 
    do{
        // exec_offset偏移量 默认从1开始,然后循环的时候从匹配到的结果开始
        rc = pcre_exec( re, NULL, subject, strlen(subject), exec_offset, 0, ovector, oveccount );
 
        if( rc > 0 ) {
            // 获取到匹配的结果
            pcre_get_substring( subject, ovector, rc, 0, &captured_string );
            printf("captured string : %s\n", captured_string);
             
            // 设置偏移量
            exec_offset = ovector[1];
            i++;
        }
    }while( rc > 0 );
 
    printf("match %d\n", i);
 
    return0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值