I already tested the regex with regex101.com and it works fine, regcomp
does not complain about a thing but the code segfaults as soon as i call regexec
. Here is the entire source file
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef regex_t *regex;
regex R(const char *pattern, int flags)
{
regex_t *reptr = malloc(sizeof(regex_t));
if (!reptr) return NULL;
if (regcomp(reptr, pattern, flags) == 0) return reptr;
free(reptr);
return NULL;
}
char *match(regex re, char *str)
{
regmatch_t match;
int reti = regexec(re, str, 1, &match, 0); // this segfaults
/* whole lotta code to return the matched token, ill take care of that myself */
}
int main()
{
char *str = strdup("%llu %s %e");
regex formatMatcher = R("%[0-9]*\.?[0-9]*(?:h{0,2}|l{0,2})(?:[acdefginopsux])|%\[.+\]", REG_EXTENDED);
match(formatMatcher, str);
regfree(formatMatcher);
free(formatMatcher);
free(str);
}
Now I know I declared a regex
in an unconvenient way and I’m ready to change that and find other solutions to make the regex declaration more readable.
I even made a match function in hopes of ease of reading. You see I tried to make main
function as easy to understand as possible.
My original intention was to extract conversion specifiers out of a string.
You see the regex I tested does so
R("%[0-9]*\.?[0-9]*(?:h{0,2}|l{0,2})(?:[acdefginopsux])|%\[.+\]", REG_EXTENDED);
I need the regex to work with REG_ICASE
flag. With that flag however, match
function doesn’t segfault, but it doesn’t match as it did with the tests I run with that regex.