The following function checks if a variable name start with a letter and may have preceding characters which are letters/ numbers. Why does the return value is always 1 no matter what the input is?
#include <regex.h>
#include <stdio.h>
int validate_var(char *str)
{
    regex_t reg;
    regcomp(®, "^[a-zA-Z]+[a-zA-Z0-9]*$", 0);
    int r = regexec(®, str, 0, NULL, 0);
    regfree(®);
    return r;
}
int main() {
    printf("%d\n", validate_var("abc")); // Reports 1, This makes sense
    printf("%d\n", validate_var("17"));  // Reports 1, This doesn't make sense
}
                You're using anchors (^ and $) but not enabling extended syntax by passing REG_EXTENDED to regcomp(). See the manual page.
You should really check all return values, there should be a failure reported somewhere due to the syntax usage error.
Note that non-zero means failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With