summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-01-09 23:43:58 +0100
committerTyge Løvset <[email protected]>2022-01-09 23:43:58 +0100
commitb1c62797f6344eaea6cb55959c9f438481359c75 (patch)
tree672e2c270bc53d025ce20dfebf072d3423487e74 /examples
parent4925e503aa2234a3ad804256713276199d58dd0d (diff)
downloadSTC-modified-b1c62797f6344eaea6cb55959c9f438481359c75.tar.gz
STC-modified-b1c62797f6344eaea6cb55959c9f438481359c75.zip
Small cregex API change. Added 2 examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/regex1.c26
-rw-r--r--examples/regex2.c26
2 files changed, 52 insertions, 0 deletions
diff --git a/examples/regex1.c b/examples/regex1.c
new file mode 100644
index 00000000..9b450453
--- /dev/null
+++ b/examples/regex1.c
@@ -0,0 +1,26 @@
+#include <stc/cstr.h>
+#include <stc/cregex.h>
+
+int main()
+{
+ c_auto (cstr, input)
+ c_auto (cregex, float_expr)
+ {
+ float_expr = cregex_new("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)");
+ // Until "q" is given, ask for another number
+ while (true)
+ {
+ printf("Enter float number (q for quit): ");
+ cstr_getline(&input, stdin);
+
+ // Exit when the user inputs q
+ if (cstr_equals(input, "q"))
+ break;
+
+ if (cregex_is_match(&float_expr, input.str))
+ printf("Input is a float\n");
+ else
+ printf("Invalid input : Not a float\n");
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/regex2.c b/examples/regex2.c
new file mode 100644
index 00000000..73abee45
--- /dev/null
+++ b/examples/regex2.c
@@ -0,0 +1,26 @@
+#include <stc/cregex.h>
+#include <stc/csview.h>
+#include <stc/cstr.h>
+
+int main()
+{
+ const char* fnames[] = {"foofile.txt", "barfile.txt", "bazboy.dat", "zoidberg"};
+ c_auto (cregex, re)
+ {
+ re = cregex_new("([a-z]+)\\.([a-z]+)");
+
+ c_forrange (i, c_arraylen(fnames))
+ {
+ printf("%s\n", fnames[i]);
+ if (cregex_is_match(&re, fnames[i]))
+ {
+ c_forrange (j, cregex_capture_size(re))
+ {
+ csview cap; cregex_capture_v(&re, j, &cap);
+ printf(" submatch %d: " c_PRIsv "\n", j, c_ARGsv(cap));
+ }
+ puts("");
+ }
+ }
+ }
+}