1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
int reg_check(const char *reg_str, const char *check_str) { const char *pattern; int i, ret, cflags = 0; char ebuf[128]; regex_t reg; regmatch_t pm[10]; const size_t nmatch = 10; int flag = 0; pattern = reg_str; ret = regcomp(®, pattern, cflags); if (ret != 0) { regerror(ret, ®, ebuf, sizeof(ebuf)); fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern); return -1; } char *check_str_t = (char *)check_str; if ((ret = strlen(check_str_t)) > 0 && check_str_t[ret - 1] == '\n') check_str_t[ret - 1] = 0; ret = regexec(®, check_str_t, nmatch, pm, 0); if (ret == REG_NOMATCH) { return -1; } else if (ret != 0) { regerror(ret, ®, ebuf, sizeof(ebuf)); fprintf(stderr, "%s: regcom('%s')\n", ebuf, check_str_t); return -1; } for (i = 0; i < nmatch && pm[i].rm_so != -1; ++i) { int one_match_len = pm[i].rm_eo - pm[i].rm_so; int str_sz = strlen(check_str_t); if (one_match_len == str_sz) { flag = 1; } break; } regfree(®); return flag == 1 ? 0 : -1; }
int json_key_check(const char *check_str) { const char *json_str = "[a-zA-Z_][a-zA-Z_0-9-]*"; return reg_check(json_str, check_str); }
|