summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regularexpressions/regex2.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/regex2.c
parent0841165881871ee01b782129be681209aeed2423 (diff)
parent1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff)
downloadSTC-modified-modified.tar.gz
STC-modified-modified.zip
Merge branch 'stclib:master' into modifiedHEADmodified
Diffstat (limited to 'misc/examples/regularexpressions/regex2.c')
-rw-r--r--misc/examples/regularexpressions/regex2.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/misc/examples/regularexpressions/regex2.c b/misc/examples/regularexpressions/regex2.c
new file mode 100644
index 00000000..a798b1a1
--- /dev/null
+++ b/misc/examples/regularexpressions/regex2.c
@@ -0,0 +1,34 @@
+#define i_import
+#include <stc/cregex.h>
+
+int main(void)
+{
+ 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!"},
+ {"(\\p{Alpha}+ )+(\\p{Nd}+)", "Großpackung süßigkeiten 199"},
+ {"\\p{Han}+", "This is Han: 王明:那是杂志吗?"},
+ };
+
+ cregex re = {0};
+ c_forrange (i, c_arraylen(s))
+ {
+ int res = cregex_compile(&re, s[i].pattern);
+ if (res < 0) {
+ printf("error in regex pattern: %d\n", res);
+ continue;
+ }
+ printf("\ninput: %s\n", s[i].input);
+
+ c_formatch (j, &re, s[i].input) {
+ c_forrange (k, cregex_captures(&re) + 1)
+ printf(" submatch %lld: %.*s\n", k, c_SV(j.match[k]));
+ }
+ }
+ cregex_drop(&re);
+}