summaryrefslogtreecommitdiffhomepage
path: root/examples/regex1.c
blob: 894fe2b1b5416543353d8f4800e806d5f9061e54 (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
#include <stc/cstr.h>
#include <stc/cregex.h>

int main(int argc, char* argv[])
{
    if (argc <= 1) {
        printf("Usage: regex1 -i\n");
        return 0;
    }
    c_auto (cstr, input)
    c_auto (cregex, float_expr)
    {
        float_expr = cregex_new("^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", 0);
        // Until "q" is given, ask for another number
        while (true)
        {
            printf("Enter a double precision number (q for quit): ");
            cstr_getline(&input, stdin);

            // Exit when the user inputs q
            if (cstr_equals(input, "q"))
                break;
            
            if (cregex_find(&float_expr, input.str, 0, NULL, 0) > 0)
                printf("Input is a float\n");
            else
                printf("Invalid input : Not a float\n");
        }
    }
}

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