summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regex_replace.c
diff options
context:
space:
mode:
author_Tradam <[email protected]>2023-09-08 01:29:47 +0000
committerGitHub <[email protected]>2023-09-08 01:29:47 +0000
commit3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch)
treeafbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/regex_replace.c
parent0841165881871ee01b782129be681209aeed2423 (diff)
parent1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff)
downloadSTC-modified-modified.tar.gz
STC-modified-modified.zip
Merge branch 'stclib:master' into modifiedHEADmodified
Diffstat (limited to 'misc/examples/regex_replace.c')
-rw-r--r--misc/examples/regex_replace.c57
1 files changed, 0 insertions, 57 deletions
diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c
deleted file mode 100644
index d3952f50..00000000
--- a/misc/examples/regex_replace.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#define i_extern
-#include <stc/cregex.h>
-#include <stc/csview.h>
-
-bool add_10_years(int i, csview match, cstr* out) {
- if (i == 1) { // group 1 matches year
- int year;
- sscanf(match.str, "%4d", &year); // scan 4 chars only
- cstr_printf(out, "%04d", year + 10);
- return true;
- }
- return false;
-}
-
-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";
- cstr str = {0};
- cregex re = {0};
-
- c_defer(
- cregex_drop(&re),
- cstr_drop(&str)
- ){
- 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"));
- 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, CREG_DEFAULT));
- printf("us+10: %s\n", cstr_str(&str));
-
- /* Wrap first date inside []: */
- cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]"));
- printf("brack: %s\n", cstr_str(&str));
-
- /* Shows how to compile RE separately */
- re = cregex_from(pattern);
- if (cregex_captures(&re) == 0)
- continue; /* break c_defer */
-
- /* European date format. */
- cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1"));
- printf("euros: %s\n", cstr_str(&str));
-
- /* Strip out everything but the matches */
- cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP));
- printf("strip: %s\n", cstr_str(&str));
-
- /* Wrap all words in ${} */
- cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}"));
- printf("curly: %s\n", cstr_str(&str));
- }
-}