I want a C ++ regular expression that matches “bananas” or “pajamas” but not “bananas2” or “banapaspayamasam” or “bananas” or nothing at all other than these two exact words. So I did this:
#include <regex.h> #include <stdio.h> int main() { regex_t rexp; int rv = regcomp(&rexp, "\\bbananas\\b|\\bpajamas\\b", REG_EXTENDED | REG_NOSUB); if (rv != 0) { printf("Abandon hope, all ye who enter here\n"); } regmatch_t match; int diditmatch = regexec(&rexp, "bananas", 1, &match, 0); printf("%d %d\n", diditmatch, REG_NOMATCH); }
and he printed 1 1 as if there was no coincidence. What happened? I also tried \bbananas\b|\bpajamas\b for my regex, and that didn't work either.
I asked for matching whole words using regex about std :: regex, but std :: regex is awful and slow, so I'm trying regex.h.
source share