delibk 2016-04-25 08:40 采纳率: 100%
浏览 1835

C语言 PCRE正则表达式 函数解析

求指教:PCRE库函数中,pcre_exec()的返回值是什么意思? *ovector数组的元素又是代表什么意

  • 写回答

2条回答 默认 最新

  • 小灸舞 2016-04-25 08:49
    关注

    int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize)
    pcre_exec()返回匹配串的偏移位置

     #include <string.h>
    #include <stdio.h>
    #include <pcre.h>
    
    int main()
    {
        pcre *re;
    
        const char *error;
        int errorOffset, i = 0;
        /**
         * pcre_exec匹配的结果
         * ovector的结构为
         * {匹配结果1的起始位置,匹配结果1的结束位置,匹配结果2的起始位置,...匹配结果N的结束位置}
         */
        int oveccount = 2, ovector[oveccount];
    
        /**
         * rc是pcre_exec匹配到的结果数量
         */
        int rc;
        /**
         * pcre_exec执行的偏移量
         * 从匹配到的结果的结束位置开始下一次匹配
         */
        int exec_offset = 0;
    
    
        const char *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);
            return 0;
        }
    
        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);
    
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog