简体   繁体   English

使用regex.h在c中进行模式匹配/提取

[英]pattern matching / extracting in c using regex.h

I need help extracting a substring from a string using regex.h in C. In this example, I am trying to extract all occurrences of character 'e' from a string 'telephone'. 我需要使用C中的regex.h从字符串中提取子字符串的帮助。在此示例中,我尝试从字符串“电话”中提取所有出现的字符“ e”。 Unfortunately, I get stuck identifying the offsets of those characters. 不幸的是,我在确定这些字符的偏移量时遇到了麻烦。 I am listing code below: 我在下面列出代码:

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

int main(void) {
    const int size=10;
    regex_t regex;
    regmatch_t matchStruct[size];

    char pattern[] = "(e)";
    char str[] = "telephone";

    int failure = regcomp(&regex, pattern, REG_EXTENDED);
    if (failure) {
        printf("Cannot compile");
    }

    int matchFailure = regexec(&regex, pattern, size, matchStruct, 0);
    if (!matchFailure) {
        printf("\nMatch!!");
    } else {
        printf("NO Match!!");
    }

    return 0;
}

So per GNU's manual, I should get all of the occurrences of 'e' when a character is parenthesized. 因此,根据GNU手册,当字符加括号时,我应该得到所有出现的“ e”。 However, I always get only the first occurrence. 但是,我总是只第一次出现。

Essentially, I want to be able to see something like: 本质上,我希望能够看到以下内容:

matchStruct[1].rm_so = 1;
matchStruct[1].rm_so = 2;

matchStruct[2].rm_so = 4;
matchStruct[2].rm_so = 5;

matchStruct[3].rm_so = 7;
matchStruct[3].rm_so = 8;

or something along these lines. 或类似的东西。 Any advice? 有什么建议吗?

Please note that you are in fact not comparing your compiled regex against str ("telephone") but rather to your plain-text pattern . 请注意,实际上您不是将编译后的正则表达式与str (“电话”)进行比较,而是与纯文本pattern Check your second attribute to regexec . 检查您的第二个属性regexec That fixed, proceed for instance to " regex in C language using functions regcomp and regexec toggles between first and second match " where the answer to your question is already given. 该问题已解决,例如,在“ C语言中使用函数regcomp和regexec在第一个和第二个匹配之间切换 ”的正则表达式中,已经给出了您问题的答案。

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

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