#include
#include
#include
#include
#include
#include
#define MAXLINE 2028
#define PACKAGE "regexample"
int main(int argc, char *argv[]) {
char line[MAXLINE];
int retval = 0;
regex_t re;
if(argc != 2) {
fprintf(stderr, "Usage: %s regularexpression < text.file\n", PACKAGE);
exit(EXIT_FAILURE);
}
setlocale(LC_ALL, "");
if(regcomp(&re, argv[1], REG_EXTENDED) != 0) {
fprintf(stderr, "%s: Error compiling regular expression: %s\n", PACKAGE, argv[1]);
exit(EXIT_FAILURE);
}
while((fgets(line, MAXLINE, stdin)) != NULL)
if((retval = regexec(&re, line, 0, NULL, 0)) == 0)
printf("%s", line);
regfree(&re);
exit(EXIT_SUCCESS);
}