summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/regularexpressions
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/regularexpressions')
-rw-r--r--misc/examples/regularexpressions/regex1.c32
-rw-r--r--misc/examples/regularexpressions/regex2.c34
-rw-r--r--misc/examples/regularexpressions/regex_match.c37
-rw-r--r--misc/examples/regularexpressions/regex_replace.c57
4 files changed, 160 insertions, 0 deletions
diff --git a/misc/examples/regularexpressions/regex1.c b/misc/examples/regularexpressions/regex1.c
new file mode 100644
index 00000000..d8032358
--- /dev/null
+++ b/misc/examples/regularexpressions/regex1.c
@@ -0,0 +1,32 @@
+#define i_import
+#include <stc/cregex.h>
+
+int main(int argc, char* argv[])
+{
+ if (argc <= 1) {
+ printf("Usage: regex1 -i\n");
+ return 0;
+ }
+ cstr input = {0};
+ cregex float_expr = {0};
+
+ int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$");
+ // Until "q" is given, ask for another number
+ if (res > 0) while (true)
+ {
+ printf("Enter a double precision number (q for quit): ");
+ cstr_getline(&input, stdin);
+
+ // Exit when the user inputs q
+ if (cstr_equals(&input, "q"))
+ break;
+
+ if (cregex_is_match(&float_expr, cstr_str(&input)))
+ printf("Input is a float\n");
+ else
+ printf("Invalid input : Not a float\n");
+ }
+
+ cstr_drop(&input);
+ cregex_drop(&float_expr);
+}
diff --git a/misc/examples/regularexpressions/regex2.c b/misc/examples/regularexpressions/regex2.c
new file mode 100644
index 00000000..a798b1a1
--- /dev/null
+++ b/misc/examples/regularexpressions/regex2.c
@@ -0,0 +1,34 @@
+#define i_import
+#include <stc/cregex.h>
+
+int main(void)
+{
+ struct { const char *pattern, *input; } s[] = {
+ {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])",
+ "date: 2024-02-29 leapyear day, christmas eve is on 2022-12-24."
+ },
+ {"(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
+ "https://en.cppreference.com/w/cpp/regex/regex_search"
+ },
+ {"!((abc|123)+)!", "!123abcabc!"},
+ {"(\\p{Alpha}+ )+(\\p{Nd}+)", "Großpackung süßigkeiten 199"},
+ {"\\p{Han}+", "This is Han: 王明:那是杂志吗?"},
+ };
+
+ cregex re = {0};
+ c_forrange (i, c_arraylen(s))
+ {
+ int res = cregex_compile(&re, s[i].pattern);
+ if (res < 0) {
+ printf("error in regex pattern: %d\n", res);
+ continue;
+ }
+ printf("\ninput: %s\n", s[i].input);
+
+ c_formatch (j, &re, s[i].input) {
+ c_forrange (k, cregex_captures(&re) + 1)
+ printf(" submatch %lld: %.*s\n", k, c_SV(j.match[k]));
+ }
+ }
+ cregex_drop(&re);
+}
diff --git a/misc/examples/regularexpressions/regex_match.c b/misc/examples/regularexpressions/regex_match.c
new file mode 100644
index 00000000..9106ffbd
--- /dev/null
+++ b/misc/examples/regularexpressions/regex_match.c
@@ -0,0 +1,37 @@
+#define i_import
+#include <stc/cregex.h>
+#define i_implement
+#include <stc/csview.h>
+
+#define i_key float
+#include <stc/cstack.h>
+
+int main(void)
+{
+ // Lets find the first sequence of digits in a string
+ const char *str = "Hello numeric world, there are 24 hours in a day, 3600 seconds in an hour."
+ " Around 365.25 days a year, and 52 weeks in a year."
+ " Boltzmann const: 1.38064852E-23, is very small."
+ " Bohrradius is 5.29177210903e-11, and Avogadros number is 6.02214076e23.";
+ cregex re = {0};
+ cstack_float vec = {0};
+
+ const char* pattern = "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?";
+ int res = cregex_compile(&re, pattern);
+ printf("%d: %s\n", res, pattern);
+
+ // extract and convert all numbers in str to floats
+ c_formatch (i, &re, str)
+ cstack_float_push(&vec, (float)atof(i.match[0].buf));
+
+ c_foreach (i, cstack_float, vec)
+ printf(" %g\n", (double)*i.ref);
+
+ // extracts the numbers only to a comma separated string.
+ cstr nums = cregex_replace_sv(&re, csview_from(str), " $0,", 0, NULL, CREG_STRIP);
+ printf("\n%s\n", cstr_str(&nums));
+
+ cstr_drop(&nums);
+ cregex_drop(&re);
+ cstack_float_drop(&vec);
+}
diff --git a/misc/examples/regularexpressions/regex_replace.c b/misc/examples/regularexpressions/regex_replace.c
new file mode 100644
index 00000000..087387d7
--- /dev/null
+++ b/misc/examples/regularexpressions/regex_replace.c
@@ -0,0 +1,57 @@
+#define i_import
+#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.buf, "%4d", &year); // scan 4 chars only
+ cstr_printf(out, "%04d", year + 10);
+ return true;
+ }
+ return false;
+}
+
+int main(void)
+{
+ 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_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));
+ }
+}