This can lead to a run as you specify regex(3) functions. The following is a trivial program corresponding to its arguments. However, if you are relatively new to C, you need to go slowly with regex(3) , since you will work with pointers and arrays and regmatch_t - offsets, lions and tigers and bears are added.;)
$ ./regexec '[[:digit:]]' 56789 alpha " " foo12bar matched: 56789 matched: foo12bar $ ./regexec '[[:digit:]](foo' error: Unmatched ( or \( $ ./regexec '[' error: Invalid regular expression
... and source:
#include <sys/types.h> #include <regex.h> #include <stdio.h> int main(int argc, char **argv) { int r; regex_t reg; ++argv; /* Danger! */ if (r = regcomp(®, *argv, REG_NOSUB|REG_EXTENDED)) { char errbuf[1024]; regerror(r, ®, errbuf, sizeof(errbuf)); printf("error: %s\n", errbuf); return 1; } for (++argv; *argv; ++argv) { if (regexec(®, *argv, 0, NULL, 0) == REG_NOMATCH) continue; printf("matched: %s\n", *argv); } return 0; }
source share