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 62 63 64 65 66 67 68 69 70 71
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <ctype.h>
int regtest(const char * regex, const char * request);
int main (void)
{
char *str_request = "a-_A2";
char *str_regex = "[-a-zA-Z0-9_]{4,1024}";
int test = 0;
test = regtest(str_regex, str_request);
return test;
}
int regtest(const char * str_regex, const char * str_request)
{
regex_t regex;
int reti;
char msgbuf[1024];
//Compile regular expression
reti = regcomp(®ex, str_regex, 0);
if( reti )
{
//erreur de compilation regex
return -1;
}
//Execute regular expression */
reti = regexec(®ex, str_request, 0, NULL, 0);
if( !reti )
{
puts("Match");
return 0;
}
else
{
if( reti == REG_NOMATCH )
{
puts("reg no match");
return 1;
}
else
{
//erreur de mémoire
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
//faire du log ici
return -2;
}
}
return -3;
} |