diff options
Diffstat (limited to 'util.c')
| -rw-r--r-- | util.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -3,6 +3,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include "util.h" @@ -34,3 +35,32 @@ ecalloc(size_t nmemb, size_t size) die("calloc:"); return p; } + +int +reMatch(const char *regex, const char *str) { + char reErr[1024] = {0}; + regex_t *re = calloc(1, sizeof(regex_t)); + int rc = 0; + + if (!re) + return -1; + + rc = regcomp(re, regex, REG_EXTENDED|REG_ICASE|REG_NOSUB); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + regfree(re); re = NULL; + fprintf(stderr, "Regex compile err: %s %s\n", regex, reErr); + return -1; + } + + rc = regexec(re, str, 0, NULL, 0); + regfree(re); re = NULL; + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex match error: %s -> %s : %s\n", + regex, str, reErr); + return -1; + } + + return rc; +} |
