From 3b616b5260f2f477563508b6eec5370506565da9 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 8 Dec 2022 16:36:30 +0100 Subject: Changed/simplified cregex_replace*() API. --- examples/regex_replace.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'examples/regex_replace.c') diff --git a/examples/regex_replace.c b/examples/regex_replace.c index e88c559f..808ac086 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -2,11 +2,11 @@ #include #include -bool add_10_years(int i, csview m, cstr* mstr) { +bool add_10_years(int i, csview match, cstr* out) { if (i == 1) { // group 1 matches year int year; - sscanf(m.str, "%4d", &year); - cstr_printf(mstr, "%04d", year + 10); + sscanf(match.str, "%4d", &year); // scan 4 chars only + cstr_printf(out, "%04d", year + 10); return true; } return false; @@ -22,23 +22,23 @@ int main() printf("INPUT: %s\n", input); /* replace with a fixed string, extended all-in-one call: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD", 0, NULL, cre_default)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD")); printf("fixed: %s\n", cstr_str(&str)); /* US date format, and add 10 years to dates: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, cre_default)); + cstr_take(&str, cregex_replace_pattern_n(pattern, input, "$1/$3/$2", 0, add_10_years, cre_default)); printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]", 1, NULL, cre_default)); + cstr_take(&str, cregex_replace_pattern_n(pattern, input, "[$0]", 1, NULL, cre_default)); printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ c_with (cregex re = cregex_from(pattern, cre_default), cregex_drop(&re)) { if (cregex_captures(&re) == 0) - continue; // break c_with + continue; /* break c_with */ /* European date format. */ - cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1", 0)); + cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); printf("euros: %s\n", cstr_str(&str)); /* Strip out everything but the matches */ @@ -47,7 +47,7 @@ int main() } /* Wrap all words in ${} */ - cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}", 0, NULL, cre_default)); + cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}")); printf("curly: %s\n", cstr_str(&str)); } } -- cgit v1.2.3