diff options
| author | Tyge Løvset <[email protected]> | 2022-07-21 13:21:05 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-21 13:21:05 +0200 |
| commit | 782fa268940611df14bce7823b4aaf6fca671b49 (patch) | |
| tree | 6d79ae8366cb842998c85aaa5aa4c99730a81092 /examples | |
| parent | 0bb2d1c27bd06ac2da72125076be82aa9166409f (diff) | |
| download | STC-modified-782fa268940611df14bce7823b4aaf6fca671b49.tar.gz STC-modified-782fa268940611df14bce7823b4aaf6fca671b49.zip | |
Moved cregex_replace*() implementation to src/cregex.c
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/regex_replace.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 377487cc..ff4b4a6a 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -17,24 +17,28 @@ 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"; - c_auto (cstr, str1, str2, str3, str4) + c_auto (cstr, str) { printf("input: %s\n", input); /* European date format */ - str1 = cregex_replace(input, pattern, "\\3.\\2.\\1"); - printf("euros: %s\n", cstr_str(&str1)); + cstr_take(&str, cregex_replace(input, pattern, "\\3.\\2.\\1")); + printf("euros: %s\n", cstr_str(&str)); /* US date format, and subtract 20 years: */ - str2 = cregex_replace_ex(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0); - printf("us-20: %s\n", cstr_str(&str2)); - - /* Wrap first date only inside []: */ - str3 = cregex_replace_ex(input, pattern, "[\\0]", NULL, 0, 1); - printf("brack: %s\n", cstr_str(&str3)); + cstr_take(&str, cregex_replace_ex(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); + printf("us-20: %s\n", cstr_str(&str)); /* replace with a fixed string: */ - str4 = cregex_replace(input, pattern, "YYYY-MM-DD"); - printf("brack: %s\n", cstr_str(&str4)); + cstr_take(&str, cregex_replace(input, pattern, "YYYY-MM-DD")); + printf("fixed: %s\n", cstr_str(&str)); + + /* Wrap first date inside []: */ + cstr_take(&str, cregex_replace_ex(input, pattern, "[\\0]", NULL, 0, 1)); + printf("brack: %s\n", cstr_str(&str)); + + /* Wrap all words in {} */ + cstr_take(&str, cregex_replace("[52] apples and [31] mangoes", "[a-z]+", "{\\0}")); + printf("curly: %s\n", cstr_str(&str)); } } |
