summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-07-21 00:00:24 +0200
committerTyge Lovset <[email protected]>2022-07-21 00:00:24 +0200
commit0bb2d1c27bd06ac2da72125076be82aa9166409f (patch)
tree712e09d3412973248fa67b9cf63ce311ea2265b5
parent6fcb91a18f3a97be0469211fffeafff533219715 (diff)
downloadSTC-modified-0bb2d1c27bd06ac2da72125076be82aa9166409f.tar.gz
STC-modified-0bb2d1c27bd06ac2da72125076be82aa9166409f.zip
Extended regex_replace.c examples.
-rw-r--r--examples/regex_replace.c10
-rw-r--r--include/stc/cregex.h2
2 files changed, 10 insertions, 2 deletions
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index 2383a119..377487cc 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -17,7 +17,7 @@ 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)
+ c_auto (cstr, str1, str2, str3, str4)
{
printf("input: %s\n", input);
/* European date format */
@@ -27,6 +27,14 @@ int main()
/* 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));
+
+ /* replace with a fixed string: */
+ str4 = cregex_replace(input, pattern, "YYYY-MM-DD");
+ printf("brack: %s\n", cstr_str(&str4));
}
}
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index a59a876c..2b972fc8 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -79,7 +79,7 @@ int cregex_compile(cregex *self, const char* pattern, int cflags);
/* number of capture groups in a regex pattern */
int cregex_captures(const cregex* self);
-/* return number of capture groups on success, or (negative) error code on failure. */
+/* return 1 on match, 0 on nomatch, and -1 on failure. */
int cregex_match(const cregex *self, const char* string,
unsigned nmatch, csview match[], int mflags);