summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regex2.c
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
committerTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
commit5f57d597cd27aef55adbcb3b452973b0c6e33667 (patch)
treedfd59c2fd0e36a6ef37912a9d0cc5a65970f1524 /misc/examples/regex2.c
parent1763be8c8cbbc0896477fcf924edd4180d1345a9 (diff)
downloadSTC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.tar.gz
STC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.zip
Restructured folders: examples, benchmarks, tests into misc folder.
Diffstat (limited to 'misc/examples/regex2.c')
-rw-r--r--misc/examples/regex2.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c
new file mode 100644
index 00000000..dbf6aae4
--- /dev/null
+++ b/misc/examples/regex2.c
@@ -0,0 +1,32 @@
+#define i_extern
+#include <stc/cregex.h>
+
+int main()
+{
+ struct { const char *pattern, *input; } s[] = {
+ {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])",
+ "date: 2024-02-29 leapyear day, christmas eve is on 2022-12-24."
+ },
+ {"(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
+ "https://en.cppreference.com/w/cpp/regex/regex_search"
+ },
+ {"!((abc|123)+)!", "!123abcabc!"}
+ };
+
+ c_auto (cregex, re)
+ c_forrange (i, c_arraylen(s))
+ {
+ int res = cregex_compile(&re, s[i].pattern, CREG_DEFAULT);
+ if (res < 0) {
+ printf("error in regex pattern: %d\n", res);
+ continue;
+ }
+ printf("input: %s\n", s[i].input);
+
+ c_formatch (j, &re, s[i].input) {
+ c_forrange (k, cregex_captures(&re))
+ printf(" submatch %lld: %.*s\n", k, c_ARGSV(j.match[k]));
+ puts("");
+ }
+ }
+}