I am trying to find out whether a full word, say test
, exists in a sentence and I’d like to do it in a case-insensitive manner. I took advantage of regex capabilities in C and used b
in the pattern. Here is the program
<code>#include <regex.h>
#include <stdio.h>
#include <string.h>
int main() {
const char* hay = "This Is a TEst, or not!";
regex_t regex;
if (regcomp(®ex, "btestb", REG_EXTENDED | REG_ICASE)) {
printf("compilation failedn");
}
regmatch_t match[1];
if (regexec(®ex, hay, 1, match, 0) == REG_NOMATCH) {
printf("couldn't matchn");
}
}
</code>
<code>#include <regex.h>
#include <stdio.h>
#include <string.h>
int main() {
const char* hay = "This Is a TEst, or not!";
regex_t regex;
if (regcomp(®ex, "btestb", REG_EXTENDED | REG_ICASE)) {
printf("compilation failedn");
}
regmatch_t match[1];
if (regexec(®ex, hay, 1, match, 0) == REG_NOMATCH) {
printf("couldn't matchn");
}
}
</code>
#include <regex.h>
#include <stdio.h>
#include <string.h>
int main() {
const char* hay = "This Is a TEst, or not!";
regex_t regex;
if (regcomp(®ex, "btestb", REG_EXTENDED | REG_ICASE)) {
printf("compilation failedn");
}
regmatch_t match[1];
if (regexec(®ex, hay, 1, match, 0) == REG_NOMATCH) {
printf("couldn't matchn");
}
}
When I run this program, it prints couldn't match
but the sentence clearly contains the TEst
word. Can someone point out the problem?