diff options
| author | Tyge Lovset <[email protected]> | 2022-07-20 23:13:11 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-07-20 23:13:11 +0200 |
| commit | 3f89c290700618eae78eaa289bdb88d1cfb3514d (patch) | |
| tree | 32dd44f8ca210aee69039f8779be611f7885cd5b /examples/regex_replace.c | |
| parent | 78cb61301df13fee995d3afd1bd1d8d63310d819 (diff) | |
| download | STC-modified-3f89c290700618eae78eaa289bdb88d1cfb3514d.tar.gz STC-modified-3f89c290700618eae78eaa289bdb88d1cfb3514d.zip | |
Added cregex_replace*() [implemented in utf8code.c]. Added examples/regex_replace.c. Docs not ready, i.e. API not fixed. Some other refactoring and minor fixes/improvements. cstr_assign_sv() now returns char* like the other cstr_assign*().
Diffstat (limited to 'examples/regex_replace.c')
| -rw-r--r-- | examples/regex_replace.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/regex_replace.c b/examples/regex_replace.c new file mode 100644 index 00000000..1216701f --- /dev/null +++ b/examples/regex_replace.c @@ -0,0 +1,35 @@ +#define i_implement +#include <stc/cstr.h> +#include <stc/cregex.h> +#include <stc/csview.h> + +cstr sub_20y(int i, csview m) { + if (i == 1) { // year + int year; + sscanf(m.str, "%4d", &year); + return cstr_from_fmt("%04d", year - 20); + } + return cstr_from_sv(m); +} + +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 (cregex, re) + c_auto (cstr, str1, str2) + { + printf("input: %s\n", input); + /* European date format */ + str1 = cregex_replace(input, pattern, "\\3.\\2.\\1"); + printf("euros: %s\n", cstr_str(&str1)); + + /* US date format, and subtract 20 years: */ + str2 = cregex_replace_fn(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0); + printf("us-20: %s\n", cstr_str(&str2)); + } +} + +#include "../src/cregex.c" +#include "../src/utf8code.c" |
