80 likes | 183 Views
CIS*2450(S04) - Seminar 4. Trouble spots from A1 Practice with Regexs POSIX Regex Library Help on Assignment #2 Python Examples. Trouble Spots from A1. 1) Free'ing stuff that you did not malloc This is dangerous! Be careful to follow assignment specifications
E N D
CIS*2450(S04) - Seminar 4 Trouble spots from A1 Practice with Regexs POSIX Regex Library Help on Assignment #2 Python Examples
Trouble Spots from A1 • 1) Free'ing stuff that you did not malloc • This is dangerous! • Be careful to follow assignment specifications • FILE * get cleaned up with fclose • 2) Spelling of findSubfield • Can submit compatibility regrade request w/reduced penalty • 3) Submitting correctly • tar czvf filename.tgz * • Submit same filename if submitting more than once
Practice with Regular Expressions • '[.](mpe?g | avi | mp[23])$' • Lines ending with .mpg, .mpeg, .avi, .mp2, .mp3 • find ~ |egrep '[.](mpe?g|avi|mp[23])$' • '^[A-Z].*[A-Z]$' • Lines beginning and ending with a capital • '^.+printf.+[(].*[){]$' • Should match first line of all 'printf' functions • Anything wrong with this regexp?
More Practice with Regular Expressions • Find all e-mails from the winter • Look for lines beginning with Date: <month> • Find all the one-line block comments in a C file • Match an unsigned byte value • Unsigned byte is [0,256) '^Date: (Jan|jan|January|january|…)' '/\*.*\*/' '^(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))$'
POSIX Regex Library (regex.h) • Three steps • 1) Compile Regex • regex_t structure • int regcomp(regex_t *compiled, const char *pattern, int cflags) • 2) Use Regex • int regexec(regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags) • 3) Clean-up Regex • void regfree(regex_t *compiled) • Cleans up the memory used internally by the struct but NOT the struct itself
POSIX Regex - Example const char *expr=argv[1]; regex_t r; int temp; temp=regcomp(&r,expr,REG_NOSUB|REG_EXTENDED); if(temp) { printf("regcomp returned non-zero. Unable to compile regex\n"); return 1; } temp=regexec(&r,argv[2],0,NULL,0); if(!temp) { printf("regex (%s) matched string (%s)\n\n",argv[1],argv[2]); } else { printf("regex (%s) DID NOT match string (%s)\n\n",argv[1],argv[2]); } regfree(&r);
Assignment #2 • Questions? • Comments? • Reminder: due on Feb 14 • Hand in structure chart: Feb 16 • Nicely printed (PPT, OpenOffice) is fine, *neatly* hand drawn is OK too. • Must match your "as built" code.
Python Some Basic Examples