Created
March 29, 2020 01:30
-
-
Save anio/86efcd5d1e95c2bf2fe5b40f3f501411 to your computer and use it in GitHub Desktop.
A Simple libc regex.h Example
This file contains hidden or 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 <stdint.h> | |
#include <string.h> | |
#include <regex.h> | |
#define MAXM 1024 | |
int main(int argc, char **argv) { | |
regex_t re; | |
regmatch_t matches[MAXM]; | |
char *input = argv[2], | |
*pattern = argv[1], | |
*cursor = input; | |
if(regcomp(&re, pattern, REG_EXTENDED)) | |
return 1; | |
while(!regexec(&re, cursor, MAXM, matches, 0)) { | |
uint32_t last_pos = 0; | |
for(uint32_t i = 0; i < MAXM; i++) { | |
size_t e = matches[i].rm_eo, | |
s = matches[i].rm_so; | |
if(s < last_pos) break; | |
if(e == -1) break; | |
char _cursor[strlen(cursor)]; | |
strcpy(_cursor, cursor); | |
_cursor[e] = 0; | |
last_pos = e; | |
printf("'%s'\n", _cursor + s); | |
cursor += last_pos; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment