/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>
#include <regex.h>

void test(const char *s, const char *pat)
{
    int stat;
    regex_t re;
    regmatch_t pmatch[2];
    char tmp[10];
    printf("%s %s\n", s, pat);
    if(regcomp(&re, pat, REG_EXTENDED) != 0)
    {
        printf("Problem compiling\n");
        return;
    }
    stat = regexec(&re, s, 2, pmatch, 0);
    if(stat == 0)
    {
        memset(tmp, 0, sizeof(tmp));
        strncpy(tmp, s + pmatch[0].rm_so, pmatch[0].rm_eo - pmatch[0].rm_so - 1);
        printf("Match: %s\n");
    }
    else if(stat == REG_BADPAT)
    {
        printf("Bad pattern\n");
    }
    else if(stat == REG_NOMATCH)
    {
        printf("No match\n");
    }
    regfree(&re);
} 

int main()
{
    test("ABC", "B");
    test("ABC", "^B$");
    test("ABC", "^ABC$");
    test("ABC", "^[A-Z]+$");
    return 0;
}
