Testing a string for a match

Regular expressions

#include <regex.h>

const char *data1 = "aaab";
const char *data2 = "aaaba";
const char *data3 = "bbba";

//REG_NOSUB: only yes or no, without the match positions
regex_t regex;
if (regcomp(&regex, "a+b", REG_EXTENDED | REG_NOSUB) != 0) {
    exit(EXIT_FAILURE);
}

//regexec returns 0 when the pattern is found anywhere
bool b1 = regexec(&regex, data1, 0, nullptr, 0) == 0;
//b1 is true

bool b2 = regexec(&regex, data2, 0, nullptr, 0) == 0;
//b2 is true

bool b3 = regexec(&regex, data3, 0, nullptr, 0) == 0;
//b3 is false

regfree(&regex);

printf("b1 is %s\n", b1 ? "true" : "false");
printf("b2 is %s\n", b2 ? "true" : "false");
printf("b3 is %s\n", b3 ? "true" : "false");