forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.c
33 lines (29 loc) · 933 Bytes
/
regex.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
/*
TODO loop over all matches.
http://stackoverflow.com/questions/16417454/why-regexec-in-posix-c-always-return-the-first-match-how-can-it-return-all-mat
*/
#include "common.h"
int main(void) {
char *src = "0a1 0a2 0a3";
char err_buf[BUFSIZ];
const char *pattern = "0.[12]";
int i;
int res;
regex_t preg;
regmatch_t pmatch[10];
if ((res = regcomp(&preg, pattern, REG_EXTENDED)) != 0) {
regerror(res, &preg, err_buf, BUFSIZ);
printf("regcomp: %s\n", err_buf);
exit(EXIT_FAILURE);
}
res = regexec(&preg, src, 10, pmatch, REG_NOTBOL);
//~ res = regexec(&preg, src, 10, pmatch, 0);
//~ res = regexec(&preg, src, 10, pmatch, REG_NOTEOL);
if (res != REG_NOMATCH) {
for (i = 0; pmatch[i].rm_so != -1; i++) {
printf("%ju %ju\n", (uintmax_t)pmatch[i].rm_so, (uintmax_t)pmatch[i].rm_eo);
}
}
regfree(&preg);
return 0;
}