Created
August 18, 2018 16:40
-
-
Save etoyoda/97075af602ce1e67ed9a2743299d17ba to your computer and use it in GitHub Desktop.
C code to test regex(3)'s behaviour especially about match data for parenthized pattern
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 <sys/types.h> | |
#include <regex.h> | |
#define NMATCH 10 | |
int | |
main(int argc, char **argv) | |
{ | |
regex_t reg; | |
regmatch_t md[NMATCH]; | |
int r, i, j; | |
if (1 < argc) { | |
r = regcomp(®, argv[1], REG_EXTENDED); | |
if (r) { puts("regcomp err"); return 2; } | |
} else { | |
puts("a.out re str ..."); | |
return 1; | |
} | |
for (i = 2; i < argc; i++) { | |
r = regexec(®, argv[i], NMATCH, md, 0); | |
if (r == REG_NOMATCH) { | |
continue; | |
} | |
if (r) { puts("regexec err"); return 3; } | |
printf("<%s>\n", argv[i]); | |
for (j = 0; j < NMATCH; j++) { | |
printf("#%d %zd..%zd (%.*s)\n", | |
j, (size_t)md[j].rm_so, (size_t)md[j].rm_eo, | |
(int)(md[j].rm_eo - md[j].rm_so), | |
argv[i] + (int)md[j].rm_so); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment