c regex not finding string -
i'm trying create collection of regexes in c, no success.
currently i'm trying find include
statements following regex:
(#include <.+>)|(#include \".+\")
here code:
#include <stdio.h> #include <stdlib.h> #include <regex.h> char *regex_str = "(#include <.+>)|(#include \".+\")"; char *str = "#include <stdio.h>"; regex_t regex; int reti; int main() { /* compile regex */ reti = regcomp(®ex, regex_str, 0); if (reti) { printf("could not compile regex.\n"); exit(1); } /* exec regex */ reti = regexec(®ex, str, 0, null, 0); if (!reti) { printf("match\n"); } else if (reti == reg_nomatch) { printf("no match\n"); } else { regerror(reti, ®ex, str, sizeof(str)); printf("regex match failed: %s\n", str); exit(1); } /* free compiled regular expression if want use regex_t again */ regfree(®ex); return 0; }
the result is: no match
what doing wrong?
you might need escape match group:
char *regex_str = "\\(#include [\"<].*[\">]\\)";
which rolled 1 pattern.
Comments
Post a Comment