/* Programmazione II - Esempio di codice Example code: POSIX regex demonstration (20000817 prelz@mi.infn.it) */ #include <stdio.h> #include <stdlib.h> /*POSIX Regexps*/ #include <regex.h> #define N_MONTHS 12 int main (int argc, char *argv[]) { regex_t regbuf; char *req_month; int retcod; char *month_regexp[N_MONTHS] = { "\\b(ja|ge|1\\b)", "\\b(fe|2\\b)", "\\b(mar|mae|3\\b)", "\\b(ap|av|4\\b)", "\\b(may|mai|mag|5\\b)", "\\b(gi|jun|juin|6\\b)", "\\b(l|jul|juil|7\\b)", "\\b(ag|ao|au|8\\b)", "\\b(se|9\\b)", "\\b(o|10\\b)", "\\b(n|11\\b)", "\\b(d|12\\b)" }; int i; if (argc<2) { fprintf(stderr,"Usage: %s <month>\n",argv[0]); exit(1); } req_month = argv[1]; for (i=0;i<N_MONTHS;i++) { if (regcomp(&regbuf,month_regexp[i],REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0) { fprintf(stderr,"%s: regcomp of %s fails.\n",argv[0],month_regexp[i]); continue; } if (regexec(&regbuf, req_month, 0,NULL,0) == 0) /* Successful match */ { printf("%s: The requested month is number %d\n",argv[0],i+1); regfree(&regbuf); exit(0); } regfree(&regbuf); } printf("Sorry: could not understand month <%s>.\n",req_month); exit(0); }