summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-01 16:43:58 +0200
committerTyge Løvset <[email protected]>2022-08-01 16:43:58 +0200
commit3038d255c230be1b0605f199a00417658a21a016 (patch)
tree164d7423ada8c9589df4c2c98ea46e533374381f /examples
parent8884747775e922e20b0646eeb29ce8a2b4a1c7cc (diff)
downloadSTC-modified-3038d255c230be1b0605f199a00417658a21a016.tar.gz
STC-modified-3038d255c230be1b0605f199a00417658a21a016.zip
start dev on v4, mainly improving API
Diffstat (limited to 'examples')
-rw-r--r--examples/regex2.c13
-rw-r--r--examples/regex_replace.c12
2 files changed, 11 insertions, 14 deletions
diff --git a/examples/regex2.c b/examples/regex2.c
index cc9464c3..384b4a36 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -4,7 +4,7 @@
int main()
{
- const char* inputs[] = {"date: 2024-02-29 leapyear day", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"};
+ const char* inputs[] = {"date: 2024-02-29 leapyear day, christmas eve is on 2022-12-24.", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"};
const char* patterns[] = {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])",
"(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
"!((abc|123)+)!",
@@ -18,14 +18,11 @@ int main()
printf("error in regex pattern: %d\n", res);
continue;
}
- csview m[20];
printf("input: %s\n", inputs[i]);
- if (cregex_find(inputs[i], &re, m, 0) == 1)
- {
- c_forrange (j, cregex_captures(&re))
- {
- printf(" submatch %" PRIuMAX ": %.*s\n", j, c_ARGsv(m[j]));
- }
+
+ c_foreach_match (m, re, inputs[i]) {
+ c_forrange (int, j, cregex_captures(&re))
+ printf(" submatch %d: %.*s\n", j, c_ARGsv(m[j]));
puts("");
}
}
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index 35b3c696..ceecf4dd 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -22,15 +22,15 @@ int main()
printf("INPUT: %s\n", input);
/* replace with a fixed string, extended all-in-one call: */
- cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD"));
+ cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD", 0));
printf("fixed: %s\n", cstr_str(&str));
/* US date format, and add 10 years to dates: */
- cstr_take(&str, cregex_replace_pe(input, pattern, "$1/$3/$2", add_10_years, 0, 0));
+ cstr_take(&str, cregex_replace_pe(input, pattern, "$1/$3/$2", 0, 0, add_10_years));
printf("us+10: %s\n", cstr_str(&str));
/* Wrap first date inside []: */
- cstr_take(&str, cregex_replace_pe(input, pattern, "[$0]", NULL, 1, 0));
+ cstr_take(&str, cregex_replace(input, pattern, "[$0]", 1));
printf("brack: %s\n", cstr_str(&str));
/* Shows how to compile RE separately */
@@ -38,16 +38,16 @@ int main()
if (cregex_captures(&re) == 0)
continue;
/* European date format. */
- cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1"));
+ cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1", 0));
printf("euros: %s\n", cstr_str(&str));
/* Strip out everything but the matches */
- cstr_take(&str, cregex_replace_re(input, &re, "$3.$2.$1;", NULL, 0, cre_r_strip));
+ cstr_take(&str, cregex_replace_ex(input, &re, "$3.$2.$1;", 0, cre_r_strip, NULL));
printf("strip: %s\n", cstr_str(&str));
}
/* Wrap all words in ${} */
- cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "$${$0}"));
+ cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "$${$0}", 0));
printf("curly: %s\n", cstr_str(&str));
}
}