diff options
| author | Tyge Løvset <[email protected]> | 2022-07-22 17:44:27 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-22 17:44:27 +0200 |
| commit | c1ecb1ac75664b0771993582fda9919e52d03a2d (patch) | |
| tree | 8206cc820313f1f1f8ae450111268e6de6811cb4 | |
| parent | 29d9d1d96d8a37f6d7e24dc170aa08a40f0f1559 (diff) | |
| download | STC-modified-c1ecb1ac75664b0771993582fda9919e52d03a2d.tar.gz STC-modified-c1ecb1ac75664b0771993582fda9919e52d03a2d.zip | |
Switched from "\\" as replacement group prefix to '$'. cregex_replace() changed: removed the last two args.
| -rw-r--r-- | examples/regex_replace.c | 8 | ||||
| -rw-r--r-- | include/stc/cregex.h | 7 | ||||
| -rw-r--r-- | src/cregex.c | 8 |
3 files changed, 13 insertions, 10 deletions
diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 2bb9517f..21252236 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -26,23 +26,23 @@ int main() 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", add_10_years, 0, 0)); 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_pe(input, pattern, "[$0]", NULL, 1, 0)); printf("brack: %s\n", cstr_str(&str)); /* European date format. Show how to compile RE separately */ cregex re = cregex_from(pattern, 0); if (cregex_captures(&re) == 0) continue; - cstr_take(&str, cregex_replace(input, &re, "\\3.\\2.\\1", NULL, 0)); + cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1")); cregex_drop(&re); printf("euros: %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}")); printf("curly: %s\n", cstr_str(&str)); } } diff --git a/include/stc/cregex.h b/include/stc/cregex.h index fff8ccb1..1b77a980 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -97,8 +97,11 @@ int cregex_match_p(const char* input, const char* pattern, csview match[], int cmflags); /* replace regular expression */ -cstr cregex_replace(const char* input, const cregex* re, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count); +cstr cregex_replace_re(const char* input, const cregex* re, const char* replace, + bool (*mfun)(int i, csview match, cstr* mstr), unsigned count); +static inline +cstr cregex_replace(const char* input, const cregex* re, const char* replace) + { return cregex_replace_re(input, re, replace, NULL, 0); } /* replace + compile RE pattern, and extra arguments */ cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, diff --git a/src/cregex.c b/src/cregex.c index 02e3c3d8..d2baf2ef 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1164,7 +1164,7 @@ build_subst_string(const char* replace, unsigned nmatch, const csview match[], cstr mstr = cstr_null; while (*replace != '\0') { - if (*replace == '\\') { + if (*replace == '$') { const char num = *++replace; int i; switch (num) { @@ -1230,8 +1230,8 @@ int cregex_match_p(const char* input, const char* pattern, } cstr -cregex_replace(const char* input, const cregex* re, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count) { +cregex_replace_re(const char* input, const cregex* re, const char* replace, + bool (*mfun)(int i, csview match, cstr* mstr), unsigned count) { cstr out = cstr_null; cstr subst = cstr_null; size_t from = 0; @@ -1258,7 +1258,7 @@ cregex_replace_pe(const char* input, const char* pattern, const char* replace, int res = cregex_compile(&re, pattern, cflags); if (res < 0) return cstr_new("[[error: invalid regex pattern]]"); - cstr out = cregex_replace(input, &re, replace, mfun, count); + cstr out = cregex_replace_re(input, &re, replace, mfun, count); cregex_drop(&re); return out; } |
