summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regularexpressions/regex_match.c
diff options
context:
space:
mode:
author_Tradam <[email protected]>2023-09-08 01:29:47 +0000
committerGitHub <[email protected]>2023-09-08 01:29:47 +0000
commit3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch)
treeafbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/regularexpressions/regex_match.c
parent0841165881871ee01b782129be681209aeed2423 (diff)
parent1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff)
downloadSTC-modified-3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd.tar.gz
STC-modified-3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd.zip
Merge branch 'stclib:master' into modifiedHEADmodified
Diffstat (limited to 'misc/examples/regularexpressions/regex_match.c')
-rw-r--r--misc/examples/regularexpressions/regex_match.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/misc/examples/regularexpressions/regex_match.c b/misc/examples/regularexpressions/regex_match.c
new file mode 100644
index 00000000..9106ffbd
--- /dev/null
+++ b/misc/examples/regularexpressions/regex_match.c
@@ -0,0 +1,37 @@
+#define i_import
+#include <stc/cregex.h>
+#define i_implement
+#include <stc/csview.h>
+
+#define i_key float
+#include <stc/cstack.h>
+
+int main(void)
+{
+ // 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.";
+ cregex re = {0};
+ cstack_float vec = {0};
+
+ 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].buf));
+
+ c_foreach (i, cstack_float, vec)
+ printf(" %g\n", (double)*i.ref);
+
+ // extracts the numbers only to a comma separated string.
+ cstr nums = cregex_replace_sv(&re, csview_from(str), " $0,", 0, NULL, CREG_STRIP);
+ printf("\n%s\n", cstr_str(&nums));
+
+ cstr_drop(&nums);
+ cregex_drop(&re);
+ cstack_float_drop(&vec);
+}