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
  • 330,190
  • 66
  • 504
  • 555
Vladimir Djukic
  • 1,932
  • 7
  • 26
  • 55

0 Answers0