summaryrefslogtreecommitdiffhomepage
path: root/examples/regex2.c
blob: 19c49ec284b0bc31c12366b1e25df0411a3bfc99 (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
33
34
35
36
37
#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
#include <stc/csview.h>

int main()
{
    const char* inputs[] = {"date: 2024-02-29 leapyear day", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"};
    const char* patterns[] = {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])",
                              "(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
                              "!((abc|123)+)!",
    };
    c_forrange (i, c_arraylen(inputs))
    {
        c_auto (cregex, re)
        {
            int res = cregex_compile(&re, patterns[i], 0);
            if (res < 0) {
                printf("error in regex pattern: %d\n", res);
                continue;
            }
            cregmatch m[20];
            printf("input: %s\n", inputs[i]);
            if (cregex_find(&re, inputs[i], 20, m, 0) > 0)
            {
                c_forrange (j, cregex_captures(re))
                {
                    printf("  submatch %" PRIuMAX ": %" c_PRIsv "\n", j, c_ARGsv(m[j]));
                }
                puts("");
            }
        }
    }
}

#include "../src/cregex.c"
#include "../src/utf8utils.c"