Created
September 19, 2024 19:37
-
-
Save denisdemaisbr/9c842d7b7a5d4f5a1770d1315097ab08 to your computer and use it in GitHub Desktop.
an example of tiny regex library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
// clone or clib | |
// https://github.com/kokke/tiny-regex-c | |
#include "tiny-regex-c/re.h" | |
#include "tiny-regex-c/re.c" | |
int main() { | |
const char *input = "joao;maria;jose"; | |
const char *pattern = "[^;]+"; | |
const char *search_pos = input; | |
int match_length; | |
printf("\n[init]\n"); | |
re_t regex = re_compile(pattern); | |
if (!regex) { | |
printf("[error] re_compile()\n"); | |
return 1; | |
} | |
match_length = 0; | |
while (re_matchp(regex, search_pos, &match_length) != -1) { | |
printf("[info] %.*s\n", match_length, search_pos); | |
search_pos += match_length; | |
if (*search_pos == ';') { | |
search_pos++; | |
} | |
if (*search_pos == '\0') { | |
break; | |
} | |
} | |
printf("[done]\n\n"); | |
return 0; | |
} | |
/* | |
$ gcc -std=c99 -g3 -O0 -o /cygdrive/x/test test.regex1.c && /cygdrive/x/test.exe | |
[init] | |
[info] joao | |
[info] maria | |
[info] jose | |
[done] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment