I am writing code in C program to check for the strength of password using regex.
My requirements are:
"atleast one upper character and one lower character one digit and one special character and overall password length should be minimum 9 characters"
First I figured out regex combination in http://regexr.com/ and regex combination is ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_+-.,!@#$%^&*();\/|<>"']).{9,})
But if I try the same in C language using program below it does not work:
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<sys/types.h>
#include<regex.h>
static void check_password_strength(const char *password) {
regex_t comp_ex;
int rc = regcomp(&comp_ex, "((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_+-.,!@#$%^&*();\/|<>"']).{9,})", 0);
if (rc != 0) {
char errbuf[1024];
(void)(regerror(rc, &comp_ex, errbuf, sizeof(errbuf)));
printf("%s: error compiling regex: %s", __FUNCTION__, errbuf);
return;
}
if (regexec(&comp_ex, password, 0, NULL, 0) == 0) {
regfree(&comp_ex);
printf("Password accepted :%s\n", password);
return;
}
printf("password NOT accepted\n");
regfree(&comp_ex);
return;
}
void main(int argc, char *argv[])
{
int i = 0;
if (argc != 2) {
printf("invalid number of args \n");
return;
}
check_password_strength(argv[1]);
}
Do I need to use regex in different way in C program? Like [[:alnum:]]
or [[:digit:]]
?
Can you please give an hint here if you know?
\
(backslash) inside the pattern. Although I agree that regex is not the way to go for this, you can simply scan the string and count each requirement... – A.S.H