2

I want to extract some part of string and include into array. Here is my current code:

  int r;
  regex_t reg;
  regmatch_t match[2];
  char *line = "[ Page 1 ]  [ Page 2asdasdasd  asdas ] [ asdasdas asdas ]";

  regcomp(&reg, "[[](.*?)[]]", 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");
  }

This will result with this:

1:  Page 1 ]  [ Page 2asdasdasd  asdas ] [ asdasdas asdas 

I need to get something like this:

array("Page 1", "Page 2asdasdasd  asdas", "asdasdas asdas");

Is it possible?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60

0 Answers0