summaryrefslogtreecommitdiffhomepage
path: root/examples/regex_match.c
blob: 72adeb8e7fce13d528634cb7986ae3850c6693f3 (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
#include <stdio.h>
#include <stc/csview.h>
#include <stc/cregex.h>
#include <stc/crandom.h>
#define i_val double
#define i_type Vecu64
#include <stc/cstack.h>
#include <time.h>


int main()
{
    // Lets find the first sequence of digits in a string
    const char *s = "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)
    {
        re = cregex_new("[+-]?([0-9]*\\.)?[0-9]+([Ee][-+]?[0-9]+)?");
        cregex_match match;
        if (cregex_find(&re, s, &match)) {
            printf("Found digits at position %zu-%zu\n", match.start, match.end);
        } else {
            printf("Could not find any digits\n");
        }

        csview sv = {0};
        while (cregex_find_next_v(&re, s, &sv)) {
            printf(c_PRIsv " ; ", c_ARGsv(sv));
        }
        puts("");
    }
}