#include <stdio.h>
#include <stdbool.h>
#include <regex.h>
bool isValidString(const char *s) {
regex_t regex;
int result;
result = regcomp(®ex, "^[A-Za-z0-9_-]*$", REG_EXTENDED);
if (result) {
fprintf(stderr, "Could not compile regex\n");
return false;
}
result = regexec(®ex, s, 0, NULL, 0);
regfree(®ex);
return result == 0;
}
int main() {
const char *s1 = "-abc_123-";
printf("%s\n", isValidString(s1) ? "yes" : "no");
const char *s2 = "-abc_123-(!)";
printf("%s\n", isValidString(s2) ? "yes" : "no");
return 0;
}
/*
run:
yes
no
*/