why regexec() of C does not match this pattern, but match() of javascript works?

10,201

For the regex library to recognize the full regular expression, use REG_EXTENDED in the regcomp flag.

it is possible to use groups?

Do you mean capturing groups? Like this?

#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

int main(void) {
  int r;
  regex_t reg;
  regmatch_t match[2];
  char *line = "----------------------- Page 1-----------------------";

  regcomp(&reg, "[-]{23}[ ]*Page[ ]*([0-9]*)[-]{23}", REG_ICASE | REG_EXTENDED);
  /*                                ^------^ capture page number */
  r = regexec(&reg, line, 2, match, 0);
  if (r == 0) {
    printf("Match!\n");
    printf("0: [%.*s]\n", match[0].rm_eo - match[0].rm_so, line + match[0].rm_so);
    printf("1: [%.*s]\n", match[1].rm_eo - match[1].rm_so, line + match[1].rm_so);
  } else {
    printf("NO match!\n");
  }

  return 0;
}
Share:
10,201
Jack
Author by

Jack

Computer lover. :-) -- I'm not a native speaker of the english language. So, if you find any mistake what I have written, you are free to fix for me or tell me on. :)

Updated on June 14, 2022

Comments

  • Jack
    Jack almost 2 years

    I have this pattern [-]{23}[ ]*Page[ ]*[0-9]*[-]{23}to extract the page number from an string like ----------------------- Page 1----------------------- it works fine using javascript regex implementation:

    var s = "----------------------- Page 1-----------------------";
    alert( s.match(/[-]{23}[ ]*Page[ ]*[0-9]*[-]{23}/) != null);
    

    match() function returns the matched string value or null if pattern does not match with string. The above code show true

    my C code:

    #include <assert.h>
    #include <sys/types.h>
    #include <regex.h>
    
    //... 
    
    regex_t reg;
    regmatch_t match;
    char * line = "----------------------- Page 1-----------------------";
    regcomp(&reg,
              "[-]{23}[ ]*Page[ ]*[0-9]*[-]{23}",
              REG_ICASE /* Don't differentiate case */
        );
    
    int r = regexec(&reg,
             line, /* line to match */
             1, /* size of captures */
             &match,
             0); 
    
    if( r == 0) { printf("Match!"); } else { printf("NO match!"); }
    

    the if-statement above print NO match! I have no idea how to fix this. Thanks in advance.