如何使用PCRE获取所有匹配组?[英] How can I use PCRE to get all match groups?

本文是小编为大家收集整理的关于如何使用PCRE获取所有匹配组?的处理方法,想解了如何使用PCRE获取所有匹配组?的问题怎么解决?如何使用PCRE获取所有匹配组?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我使用c缺乏经验,我需要使用pcre来获得匹配.
以下是我的源代码示例:

int test2()
{
    const char *error;
    int   erroffset;
    pcre *re;
    int   rc;
    int   i;
    int   ovector[OVECCOUNT];

    char *regex = "From:([^@]+)@([^\r]+)";
    char str[]  = "From:regular.expressions@example.com\r\n"\
                  "From:exddd@43434.com\r\n"\
                  "From:7853456@exgem.com\r\n";

    re = pcre_compile (
             regex,       /* the pattern */
             0,                    /* default options */
             &error,               /* for error message */
             &erroffset,           /* for error offset */
             0);                   /* use default character tables */

    if (!re) {
        printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
        return -1;
    }

    rc = pcre_exec (
        re,                   /* the compiled pattern */
        0,                    /* no extra data - pattern was not studied */
        str,                  /* the string to match */
        strlen(str),          /* the length of the string */
        0,                    /* start at offset 0 in the subject */
        0,                    /* default options */
        ovector,              /* output vector for substring information */
        OVECCOUNT);           /* number of elements in the output vector */

    if (rc < 0) {
        switch (rc) {
            case PCRE_ERROR_NOMATCH:
                printf("String didn't match");
                break;

            default:
                printf("Error while matching: %d\n", rc);
                break;
        }
        free(re);
        return -1;
    }

    for (i = 0; i < rc; i++) {
        printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]);
    }
}

在此演示中,输出仅限:

0: From:regular.expressions@example.com
1: regular.expressions
2: example.com

我想输出所有匹配项;我该怎么做?

推荐答案

我使用类来包装pcre以使这更容易,但在pcre_exec之后,overtor包含您需要在原始字符串中找到匹配项的子字符串索引.

所以它会是如下:

#include <string>
#include <iostream>
#include "pcre.h"

int main (int argc, char *argv[])
{
    const char *error;
    int   erroffset;
    pcre *re;
    int   rc;
    int   i;
    int   ovector[100];

    char *regex = "From:([^@]+)@([^\r]+)";
    char str[]  = "From:regular.expressions@example.com\r\n"\
                  "From:exddd@43434.com\r\n"\
                  "From:7853456@exgem.com\r\n";

    re = pcre_compile (regex,          /* the pattern */
                       PCRE_MULTILINE,
                       &error,         /* for error message */
                       &erroffset,     /* for error offset */
                       0);             /* use default character tables */
    if (!re)
    {
        printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
        return -1;
    }

    unsigned int offset = 0;
    unsigned int len    = strlen(str);
    while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0)
    {
        for(int i = 0; i < rc; ++i)
        {
            printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]);
        }
        offset = ovector[1];
    }
    return 1;
}

其他推荐答案

注意:pcre_exec()的最后一个参数必须是元素计数,而不是sizeof()! ( http://www.pcre.org/readme.txt )

本文地址:https://www.itbaoku.cn/post/359106.html