diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/regex2.c | 4 | ||||
| -rw-r--r-- | examples/regex_match.c | 1 | ||||
| -rw-r--r-- | examples/regex_replace.c | 28 |
3 files changed, 19 insertions, 14 deletions
diff --git a/examples/regex2.c b/examples/regex2.c index 7c17e663..23f774ba 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -1,6 +1,6 @@ +#define i_implement #include <stc/cstr.h> #include <stc/cregex.h> -#include <stc/csview.h> int main() { @@ -18,7 +18,7 @@ int main() printf("error in regex pattern: %d\n", res); continue; } - cregmatch m[20]; + csview m[20]; printf("input: %s\n", inputs[i]); if (cregex_match(inputs[i], &re, m, 0) == 1) { diff --git a/examples/regex_match.c b/examples/regex_match.c index 646f2318..821813bb 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -1,3 +1,4 @@ +#define i_implement #include <stc/cstr.h> #include <stc/cregex.h> diff --git a/examples/regex_replace.c b/examples/regex_replace.c index d57dede8..0b6c9a1e 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -1,7 +1,6 @@ #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 @@ -19,26 +18,31 @@ int main() c_auto (cstr, str) { - printf("input: %s\n", input); - /* European date format */ - cstr_take(&str, cregex_replace_pat(input, pattern, "\\3.\\2.\\1")); - printf("euros: %s\n", cstr_str(&str)); + printf("input: %s\n\n", input); + + /* replace with a fixed string, extended all-in-one call: */ + cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD")); + printf("fixed: %s\n", cstr_str(&str)); /* US date format, and subtract 20 years: */ - cstr_take(&str, cregex_replace_patx(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); + cstr_take(&str, cregex_replace_pe(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); printf("us-20: %s\n", cstr_str(&str)); - /* replace with a fixed string: */ - cstr_take(&str, cregex_replace_pat(input, pattern, "YYYY-MM-DD")); - printf("fixed: %s\n", cstr_str(&str)); - /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_patx(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)); /* Wrap all words in {} */ - cstr_take(&str, cregex_replace_pat("[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)); + + /* European date format, 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)); + cregex_drop(&re); + printf("euros: %s\n", cstr_str(&str)); } } |
