ColrC  0.4.0
An easy to use C library for linux terminal colors/escape-codes.
colr_replace_re_all_example.c
#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 was foo, and I mean foo.";
char* pattern = "fo{2}";
/*
Replace all regex matches with a string.
*/
char* replaced = colr_replace_re_all(
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 all regex matches with a ColorText.
*/
replaced = colr_replace_re_all(
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 all regex matches with a ColorResult.
*/
replaced = colr_replace_re_all(
mystring,
pattern,
" ",
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 all regex matches with a ColorResult.
*/
char* mytemplate = "This REDis " NC "kinda REDuseful?" NC;
replaced = colr_replace_re_all(
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;
}