summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regex_match.c
blob: b7d6ed3a2c008dcf5fc34804b8bf125bb2a0b95c (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
#define i_extern
#include <stc/cregex.h>
#include <stc/csview.h>

#define i_val float
#include <stc/cstack.h>

int main()
{
    // Lets find the first sequence of digits in a string
    const char *str = "Hello numeric world, there are 24 hours in a day, 3600 seconds in an hour."
                      " Around 365.25 days a year, and 52 weeks in a year."
                      " Boltzmann const: 1.38064852E-23, is very small."
                      " Bohrradius is 5.29177210903e-11, and Avogadros number is 6.02214076e23.";

    c_AUTO (cregex, re)
    c_AUTO (cstack_float, vec)
    c_AUTO (cstr, nums)
    {
        const char* pattern = "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?";
        int res = cregex_compile(&re, pattern);
        printf("%d: %s\n", res, pattern);

        // extract and convert all numbers in str to floats
        c_FORMATCH (i, &re, str)
            cstack_float_push(&vec, (float)atof(i.match[0].str));

        c_FOREACH (i, cstack_float, vec)
            printf("  %g\n", *i.ref);

        // extracts the numbers only to a comma separated string.
        nums = cregex_replace_sv(&re, csview_from(str), " $0,", 0, NULL, CREG_R_STRIP);
        printf("\n%s\n", cstr_str(&nums));
    }
}