How to match any single character in a string using regular expression with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <regex.h>

int match(const char *string, const char *pattern) {
    regex_t regex;
    int result;

    regcomp(&regex, pattern, 0);
    result = regexec(&regex, string, 0, NULL, 0);
    regfree(&regex);

    return result == 0;
}

int main() {
    printf("%d\n", match("bud", "b.d"));
    printf("%d\n", match("bid", "b.d"));
    printf("%d\n", match("bed", "b.d"));
    printf("%d\n", match("b d", "b.d"));
    printf("%d\n", match("bat", "b.d"));
    printf("%d\n", match("bd", "b.d"));
    printf("%d\n", match("bead", "b.d"));

    return 0;
}


/*
run:

1
1
1
1
0
0
0

*/

 



answered Feb 15, 2025 by avibootz
...