Вопрос

Got a little problem in a function while iterating between FALSE and TRUE. If I use the function as main function it works (the PCRE pattern matches ok), when I want to call that function from main() then I have a problem and is no match. I think I did some logic error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcre.h>

typedef enum {FALSE, TRUE} bool;

static const char sub[] =
  "12345678901234567890^otherstrings^and^digits12345678901234567890";

bool CheckMyDigit(const char *subject)
{
  static const char my_pattern[] = "([0-9]{20})(?=[\\^=])";
  const char *errtext = NULL;
  int errofs = 0;
  pcre* recc = pcre_compile(my_pattern, 0, &errtext, &errofs, NULL);
  if (recc != NULL)
  {
    int ovcc[9];
    int rccc = pcre_exec(recc, NULL, subject, sizeof(subject), 0, 0, ovcc, 9);
    if (rccc >= 0)
    {
      const char *spcc = NULL;
      pcre_get_substring(subject, ovcc, rccc, 0, &spcc);
      printf("%s\n", spcc);
      pcre_free_substring(spcc);
      return TRUE;
    }
    else
    {
      return FALSE;
    }
  }

  pcre_free(recc);
}

int main()
{

  if(CheckMyDigit(sub) == TRUE) {
    printf("Match!\n");
  }
  else
  {
    printf("Error!\n");
  }
  return 0;
}

Any idea where am I wrong?

Это было полезно?

Решение

Problem is, you are using sizeof instead of string length :

pcre_exec(recc, NULL, subject, sizeof(subject), 0, 0, ovcc, 9
                               ^^^^^^^^^^^^^^^ 

It should be:

pcre_exec(recc, NULL, subject, strlen(subject), 0, 0, ovcc, 9

It runs in main() function where I think you uses sub array it self and sizeof(sub) gives number of chars + 1, but in function you sizeof(subjest) == sizeof (char*) in your system.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top