bool validate_string(const char* str, const char* regex_pattern)
{
- regex_t* regex = malloc(sizeof(regex_t));
- if (regex == NULL)
- return false;
-
- int comp_success = regcomp(regex, regex_pattern, REG_NOSUB | REG_EXTENDED);
- if (comp_success != 0) {
- free(regex);
- return false;
- }
-
- int match = regexec(regex, str, 0, NULL, 0);
-
- regfree(regex);
- free(regex);
- regex = NULL;
+ regex_t regex;
- if (match == REG_NOMATCH)
+ int comp_success = regcomp(®ex, regex_pattern, REG_NOSUB | REG_EXTENDED);
+ if (comp_success != 0)
return false;
- if (match == 0)
- return true;
+ int match = regexec(®ex, str, 0, NULL, 0);
- return false;
+ regfree(®ex);
+ return match == REG_NOERROR;
}
bool validate_datestring(const char* date)