This may get you started, as you indicate regex(3)
functions. Following is a trivial program matching its arguments. However, if you're relatively new to C, you'll want to go slowly with regex(3)
, as you'll be working with pointers and arrays and regmatch_t
-supplied offsets and lions and tigers and bears. ;)
$ ./regexec '[[:digit:]]' 56789 alpha " " foo12bar
matched: 56789
matched: foo12bar
$ ./regexec '[[:digit:]](foo'
error: Unmatched ( or \(
$ ./regexec '['
error: Invalid regular expression
... and the 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;
}