2

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.

Jack
  • 16,276
  • 55
  • 159
  • 284
  • 3
    Try adding (ORing) the `REG_EXTENDED` flag ( http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html ). – pmg Mar 11 '12 at 15:24
  • @pmg:Thanks a lot. Was worked fine. another question, it is possible to use `groups`? – Jack Mar 11 '12 at 15:32

1 Answers1

13

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;
}
pmg
  • 106,608
  • 13
  • 126
  • 198