c - 如何使用 PCRE 获取所有匹配组?

标签 c regex pattern-matching 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: 来自:regular.expressions@example.com
1: 正则表达式
2: example.com

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

最佳答案

我使用一个类来包装 PCRE 以使其更容易,但在 pcre_exec 之后,ovector 包含子字符串索引,您需要在原始字符串中找到匹配项。

所以它会是这样的:

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

关于c - 如何使用 PCRE 获取所有匹配组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1421785/

相关文章:

c - pthread_create 更改数组的值

php - preg_replace 密码过滤器

regex - 使用正则表达式删除子字符串直到第一个 token

scala - scala中的类型级别模式匹配

python - 为文本挖掘映射关系挖掘维基百科

c++ - 如何摆脱这个汇编代码?

c - 如何按数字 block 打印 int 数组? (直方图打印)

c - 获取 pcap 捕获文件中的数据包数量?

python - Python 正则表达式评估期间的空分组

lisp - Racket 匹配语法准引号和问号