From 8d5a9ea8b743253bd33b6ecca8e7e4e650aa6f07 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 6 Jan 2022 21:21:54 +0100 Subject: Added customized version of the awesome regex lib by Fabian van Rissenbeck. --- examples/regex_match.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/regex_match.c (limited to 'examples') diff --git a/examples/regex_match.c b/examples/regex_match.c new file mode 100644 index 00000000..dad28f84 --- /dev/null +++ b/examples/regex_match.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + + +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. That's it."; + + c_auto (cregex, re) + { + re = cregex_compile("[+-]?([0-9]*\\.)?[0-9]+([Ee][-+]?[0-9]+)?"); + cregex_match_t m; + if (cregex_match(re, s, &m)) { + printf("Found digits at position %u-%u\n", m.match_begin, m.match_end); + } else { + printf("Could not find any digits\n"); + } + + c_auto (cregex_res, matches) { + matches = cregex_match_all(re, s); + csview sv = csview_from(s); + c_foreach (i, cregex_res, matches) { + csview r = csview_slice(sv, i.ref->match_begin, i.ref->match_end); + printf(c_svfmt " / ", c_svarg(r)); + } + } + puts(""); + } +} \ No newline at end of file -- cgit v1.2.3