Created
February 1, 2022 12:10
-
-
Save andlrc/d87762391a67fd13c19646759094dbdd to your computer and use it in GitHub Desktop.
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 <err.h> | |
#include <regex.h> | |
#include <stdio.h> | |
#include <string.h> | |
void redie(int eval, regex_t *re, char *msg) { | |
char buf[1024]; | |
regerror(eval, re, buf, sizeof(buf)); | |
errx(1, "%s: %s", msg, buf); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
regex_t re; | |
regmatch_t match[10]; | |
int eval; | |
char *str = argv[1] ? argv[1] : " hello"; | |
eval = regcomp(&re, "^ *| *$", REG_EXTENDED); | |
if (eval) { | |
redie(eval, &re, "regcomp"); | |
} | |
match[0].rm_so = 0; | |
match[0].rm_eo = strlen(str); | |
printf("--- [%lld,%lld] --- <%s>", match[0].rm_so, match[0].rm_eo, str); | |
eval = regexec(&re, str, 10, match, REG_STARTEND); | |
printf("--- %d [%lld,%lld]\n", eval, match[0].rm_so, match[0].rm_eo); | |
if (eval) { | |
redie(eval, &re, "regexec: 0"); | |
} | |
match[0].rm_so = match[0].rm_eo; | |
match[0].rm_eo = strlen(str); | |
printf("--- [%lld,%lld] --- <%s>", match[0].rm_so, match[0].rm_eo, str); | |
eval = regexec(&re, str, 10, match, REG_NOTBOL | REG_STARTEND); | |
printf("--- %d [%lld,%lld]\n", eval, match[0].rm_so, match[0].rm_eo); | |
if (eval) { | |
redie(eval, &re, "regexec: 1"); | |
} | |
regfree(&re); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On macOS the above outputs: