简体   繁体   English

如何使用 C 正则表达式提取子匹配

[英]How can I extract submatches using C regex

I have made GNU regex library work exactly as advertised in an extensive text processing alg that I wrote about 2 years ago, but unfortunately that platform is gone and I don't know whether its versions were older or newer than that referenced below.我已经使 GNU 正则表达式库的工作方式与我在大约 2 年前编写的一个广泛的文本处理算法中所宣传的完全一样,但不幸的是,该平台已经消失,我不知道它的版本是比下面引用的版本旧还是新。

Here is the code:这是代码:

// GNU libc version: 2.28
// gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

#include <stdio.h>
#include <regex.h>

int main() {

    regex_t preg;
    char str[] = "dave";
    char regex[] = "\\(.\\)ave";

    // flag REG_EXTENDED with unescaped parens in the r.e. doesn't fix anything
    int ret, cflags = REG_ICASE;
    
    // the elements of unused pmatches used to be set to -1 by regexec, but no longer. a clue perhaps.

    regmatch_t pmatch[2] = {{-1,-1},{-1,-1}};

    ret = regcomp(&preg, regex, cflags);
    if (ret) {
        puts("regcomp fail");
        return ret;
    }
    else
        // preg.re_nsub contains the correct number of groups that regcomp recognized in the r.e. Tests succeeded for 0, 1, 2, and 3 groups.
        printf("regcomp ok; re_nsub=%zu\n", preg.re_nsub);

    ret = regexec(&preg, str, 1, pmatch, 0);

    if(ret)
        puts("no match");
    else {
        printf("match offsets are %d %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
        printf("match[0]=%*s<\n", pmatch[0].rm_eo, &str[pmatch[0].rm_so]);

        printf("submatch offsets are %d %d\n", pmatch[1].rm_so, pmatch[1].rm_eo);
        if(pmatch[1].rm_so != -1)
            printf("match[1]=%*s<\n", pmatch[1].rm_eo, &str[pmatch[1].rm_so]);
    }
    return 0;
}
/*
output:
regcomp ok; re_nsub=1
match offsets are 0 4
match[0]=dave<
submatch offsets are -1 -1
*/

The issue you do not get offsets for the first capturing group is that you pass 1 as the third, size_t __nmatch , argument to regexec .您没有获得第一个捕获组的偏移量的问题是您将1作为第三个size_t __nmatch参数传递给regexec

The 1 value should be changed to 2 as there will be two groups whenever \(.\)ave regex matches: Group 0 will be holding the whole match and Group 1 will hold the first capturing group value. 1值应更改为2 ,因为每当\(.\)ave正则表达式匹配时将有两个组:第 0 组将保存整个匹配,第 1 组将保存第一个捕获组值。

So, you need to use所以,你需要使用

ret = regexec(&preg, str, 2, pmatch, 0);
//                       ^^^

Also, to print the Group 1 value you can use此外,要打印第 1 组值,您可以使用

if(pmatch[1].rm_so != -1) {
    printf("match[1]=%.*s<\n", pmatch[1].rm_eo, &str[pmatch[1].rm_so]);
}

See this C demo :请参阅此 C 演示

#include <stdio.h>
#include <regex.h>
#include <string.h>

int main() {

    regex_t preg;
    char str[] = "dave";
    char regex[] = "\\(.\\)ave";

    // flag REG_EXTENDED with unescaped parens in the r.e. doesn't fix anything
    int ret, cflags = REG_ICASE;
    
    // the elements of unused pmatches used to be set to -1 by regexec, but no longer. a clue perhaps.

    regmatch_t pmatch[2] = {{-1,-1},{-1,-1}};

    ret = regcomp(&preg, regex, cflags);
    if (ret) {
        puts("regcomp fail");
        return ret;
    }
    else
        // preg.re_nsub contains the correct number of groups that regcomp recognized in the r.e. Tests succeeded for 0, 1, 2, and 3 groups.
        printf("regcomp ok; re_nsub=%zu\n", preg.re_nsub);

    ret = regexec(&preg, str, 2, pmatch, 0); // 1 changed to 2 as there is Group 0 (whole match) and Group 1 (for the first capturing group)

    if(ret)
        puts("no match");
    else {
        printf("match offsets are %d %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
        printf("match[0]=%*s<\n", pmatch[0].rm_eo, &str[pmatch[0].rm_so]);

        printf("submatch offsets are %d %d\n", pmatch[1].rm_so, pmatch[1].rm_eo);
        if(pmatch[1].rm_so != -1) {
            printf("match[1]=%.*s<\n", pmatch[1].rm_eo, &str[pmatch[1].rm_so]);
        }
    }
    return 0;
}
/*
regcomp ok; re_nsub=1
match offsets are 0 4
match[0]=dave<
submatch offsets are 0 1
match[1]=d<
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM