summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regex2.c
blob: 55f8cfc2675e8e566874e09e59c02ee511e2ba5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define i_extern
#include <stc/cregex.h>

int main()
{
    struct { const char *pattern, *input; } s[] = {
        {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])",
         "date: 2024-02-29 leapyear day, christmas eve is on 2022-12-24."
        },
        {"(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
         "https://en.cppreference.com/w/cpp/regex/regex_search"
        },
        {"!((abc|123)+)!", "!123abcabc!"}
    };

    c_AUTO (cregex, re)
    c_FORRANGE (i, c_ARRAYLEN(s))
    {
        int res = cregex_compile(&re, s[i].pattern, CREG_DEFAULT);
        if (res < 0) {
            printf("error in regex pattern: %d\n", res);
            continue;
        }
        printf("input: %s\n", s[i].input);

        c_FORMATCH (j, &re, s[i].input) {
            c_FORRANGE (k, cregex_captures(&re))
                printf("  submatch %lld: %.*s\n", k, c_ARGSV(j.match[k]));
            puts("");
        }
    }
}