#include #include #include #include FILE *fp=NULL; int main(int argc, char ** argv){ char line[100]; int n=100; regex_t reg[100]; regex_t *preg=reg; size_t nmatch=0; regmatch_t *pmatch; if (argc<3) { fprintf(stderr,"%s \n", argv[0]); fprintf(stderr,"This program searches for the regual expressions in th text file. It prints the line if the regular expression is found, otherwise not. Similar to grep!\n" exit(0); } fp=fopen(argv[1],"r"); if (!fp) { fprintf(stderr,"Cannot open %s.", argv[1]); exit(0); } regcomp(preg, (const char *) argv[2], REG_EXTENDED | REG_NOSUB ); while (!feof(fp)){ fgets(line,n,fp); if ( regexec(preg, line ,nmatch , pmatch, REG_NOTBOL)!=REG_NOMATCH){ fprintf(stderr,"%s",line); } } if (fp) fclose(fp); regfree(preg); return 0; }