-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolr_replace_re_example.c
82 lines (74 loc) · 2.01 KB
/
colr_replace_re_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "colr.h"
int main(void) {
/*
If you already have a `NULL`-terminated array of `regmatch_t` (`regmatch_t**`),
a single `regex_t`, or a compiled regex pattern (`regex_t`),
you can use colr_replace() or colr_replace_all().
This macro (colr_replace_re_all) is for string patterns.
*/
// The string we are modifying.
char* mystring = "This is a foo line.";
char* pattern = "fo{2}";
/*
Replace a regex match with a string.
*/
char* replaced = colr_replace_re(
mystring,
pattern,
"replaced",
0
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a regex match with a ColorText.
*/
replaced = colr_replace_re(
mystring,
pattern,
Colr("replaced", fore(RED)),
REG_ICASE
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a regex match with a ColorResult.
*/
replaced = colr_replace_re(
mystring,
pattern,
Colr_join(
" ",
Colr("really", style(BRIGHT)),
Colr("replaced", fore(BLUE))
),
0
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a regex match with a ColorResult.
*/
char* mytemplate = "This is REDuseful" NC "?";
replaced = colr_replace_re(
mytemplate,
"RED",
fore(RED),
0
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
return EXIT_SUCCESS;
}