diff options
| author | Tyge Løvset <[email protected]> | 2022-08-02 18:16:26 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-02 18:16:26 +0200 |
| commit | 2329ff3b35d80a4c357708ed9456d72f6db6a5dd (patch) | |
| tree | 2eceede436a23dbbb44de14caa89aae2774d92d5 /docs/cregex_api.md | |
| parent | 9a9b6b2b7b7931bf9ad301ba34aa53c341ec2505 (diff) | |
| download | STC-modified-2329ff3b35d80a4c357708ed9456d72f6db6a5dd.tar.gz STC-modified-2329ff3b35d80a4c357708ed9456d72f6db6a5dd.zip | |
Updated cregex docs.
Diffstat (limited to 'docs/cregex_api.md')
| -rw-r--r-- | docs/cregex_api.md | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md index a71ae31b..f95b8114 100644 --- a/docs/cregex_api.md +++ b/docs/cregex_api.md @@ -39,13 +39,13 @@ int cregex_find_p(const char* input, const char* pattern, csview match[] bool cregex_is_match(const char* input, const cregex* re, int mflags); -cstr cregex_replace(const char* input, const cregex* re, const char* replace); -cstr cregex_replace_re(const char* input, const cregex* re, const char* replace, // extended args: - bool (*mfun)(int grp, csview match, cstr* mstr), unsigned count, int rflags); +cstr cregex_replace(const char* input, const cregex* re, const char* replace, unsigned count); +cstr cregex_replace_ex(const char* input, const cregex* re, const char* replace, unsigned count, + int rflags, bool (*mfun)(int grp, csview match, cstr* mstr)); // takes string pattern instead of re -cstr cregex_replace_p(const char* input, const char* pattern, const char* replace); -cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, - bool (*mfun)(int grp, csview match, cstr* mstr), unsigned count, int crflags); +cstr cregex_replace_p(const char* input, const char* pattern, const char* replace, unsigned count); +cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, unsigned count, + int crflags, bool (*mfun)(int grp, csview match, cstr* mstr)); void cregex_drop(cregex* self); // destroy ``` @@ -125,6 +125,26 @@ if (cregex_find_p(input, pattern, match, 0)) To compile, use: `gcc first_match.c src/cregex.c src/utf8code.c`. In order to use a callback function in the replace call, see `examples/regex_replace.c`. +### Iterate through matches, c_foreach_match + +To iterate multiple matches in an input string, you may use: +```c +csview match[5] = {0}; +while (cregex_find(input, &re, match, cre_m_next) == cre_success) { + c_forrange (int, i, cregex_captures(&re)) + printf("submatch %d: %.*s\n", i, c_ARGsv(match[i])); + puts(""); +} +``` +There is also a safe macro that simplifies it a bit: +```c +c_foreach_match (m, &re, input) { + c_forrange (int, i, cregex_captures(&re)) + printf("submatch %d: %.*s\n", i, c_ARGsv(m.ref[i])); + puts(""); +} +``` + ## Using cregex in a project **cregex** uses the following files: |
