cregexpcreposix-api

Grouping while using regexec


I have an input string like 051916.000. I would like to segregate 05, 19, 16 and 000. I am trying to use regexec in this way in C language.

regex_t r;
regmatch_t pmatch[4];
char* pattern = "/([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
int status = regcomp(&r, "", REG_EXTENDED|REG_NEWLINE);
status = regexec(&r, t, 4, pmatch, 0);
regfree(&r);

But this does not seem to work. Below is the GDB output

(gdb) p pmatch 
$1 = {{rm_so = 0, rm_eo = 0}, {rm_so = -1, rm_eo = -1}, {rm_so = -1, rm_eo = -1}, {rm_so = -1, rm_eo = -1}}

I have used Regex in Python. I am new to Regex in C. So I am not sure where I am going wrong. Regex is verified, and it matches correctly.


Solution

  • There are some minor errors here:

    char* pattern = "/([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
    

    You've got a leading slash. Regexes here are composed without surrounding slashes; remove it.

    status = regcomp(&r, "", REG_EXTENDED|REG_NEWLINE);
    

    Here, you pass an empty string as pattern. You want to pass ´pattern`, of course.

    regmatch_t pmatch[4];
    

    If you want to capture all four bracketed subexpressions, you should pass an array of size 5: pmatch[0] is the whole expression.

    When you fix these, your code works:

    const char *t = "051916.000";
    regex_t r;
    regmatch_t pmatch[5];
    char* pattern = "([0-9]{2})([0-9]{2})([0-9]{2})\\.(.*)";
    int status, i;
    
    status = regcomp(&r, pattern, REG_EXTENDED|REG_NEWLINE);
    if (status == 0) status = regexec(&r, t, 5, pmatch, 0);
    
    if (status == 0) {
        for (i = 0; i < 5; i++) {
            int len = pmatch[i].rm_eo - pmatch[i].rm_so;
            const char *str = t + pmatch[i].rm_so;
    
            printf("'%.*s'\n", len, str);
        }
    }
    
    regfree(&r);