#include <stdio.h>
#include <string.h>
#include <regex.h>
#include <stdbool.h>
// b[aeou]y: This pattern looks for strings that match the following:
// b: The letter "b".
// [aeou]: Any single character that is either "a", "e", "o", or "u".
// y: The letter "y".
bool checkPattern(const char *pattern, const char *text) {
regex_t re;
bool result = false;
if (regcomp(&re, pattern, REG_EXTENDED) != 0) {
fprintf(stderr, "Could not compile regex\n");
return false;
}
if (regexec(&re, text, 0, NULL, 0) == 0) {
result = true;
}
regfree(&re);
return result;
}
int main() {
const char *pattern = "b[aeou]y";
printf("%d\n", checkPattern(pattern, "A smart boy")); // boy
printf("%d\n", checkPattern(pattern, "I want to buy this laptop")); // buy
printf("%d\n", checkPattern(pattern, "baay"));
printf("%d\n", checkPattern(pattern, "baeouy"));
printf("%d\n", checkPattern(pattern, "baey"));
printf("%d\n", checkPattern(pattern, "This is beauty"));
printf("%d\n", checkPattern(pattern, "A programming book"));
return 0;
}
/*
run:
1
1
0
0
0
0
0
*/