summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regex_replace.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/regex_replace.c')
-rw-r--r--misc/examples/regex_replace.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c
index ebb57488..d3952f50 100644
--- a/misc/examples/regex_replace.c
+++ b/misc/examples/regex_replace.c
@@ -16,9 +16,13 @@ int main()
{
const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b";
const char* input = "start date: 2015-12-31, end date: 2022-02-28";
+ cstr str = {0};
+ cregex re = {0};
- c_auto (cstr, str)
- {
+ c_defer(
+ cregex_drop(&re),
+ cstr_drop(&str)
+ ){
printf("INPUT: %s\n", input);
/* replace with a fixed string, extended all-in-one call: */
@@ -34,17 +38,17 @@ int main()
printf("brack: %s\n", cstr_str(&str));
/* Shows how to compile RE separately */
- c_with (cregex re = cregex_from(pattern), cregex_drop(&re)) {
- if (cregex_captures(&re) == 0)
- continue; /* break c_with */
- /* European date format. */
- cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1"));
- printf("euros: %s\n", cstr_str(&str));
-
- /* Strip out everything but the matches */
- cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP));
- printf("strip: %s\n", cstr_str(&str));
- }
+ re = cregex_from(pattern);
+ if (cregex_captures(&re) == 0)
+ continue; /* break c_defer */
+
+ /* European date format. */
+ cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1"));
+ printf("euros: %s\n", cstr_str(&str));
+
+ /* Strip out everything but the matches */
+ cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP));
+ printf("strip: %s\n", cstr_str(&str));
/* Wrap all words in ${} */
cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}"));