15. Regexps
make 15_Regexp subdirectory. All code should reside there.
Look at lecture and try
to teacher: select appropriate subset of examples
examples of searching/matching — with grep
examples of search/replace — with sed 's/regexp/replacement/'
convert them to sed -E
Search with fortune -l output
"You" and "you" (use grep)
Any prononus from "you, me, he, she" list (with egrep --color, you will need an alternative)
try to match exact word (not any word part)
hint1: "[^[:alpha:]]" is non-letter character
hint2: "(^|a)word" matches both word at the beginning of the line or aword everywhere in the line.
(optional if there's time) Search/replace practice TODO
Using C regex
This is mandatory part
readlines.c acting as simple cat; using getline is mandatory
./readlines file prints all lines of file
notice repeating line=NULL, len=0 getline() parameters to make this work
do not forget to free() each line
relines.c acting as simple grep
reshow.c that takes in account grouping operations in pattern (less then than #define MAXGR 10)
See more on regexec:
each pmatch[] element is start and end of substring matching certain regexp
pmatch[0] is for whole regexp
pmatch[1] is for 1st group
pmatch[2] is for 2nd group
- …
- Use this code:
Suppose file textfile is
ab abba 00aaaEEEbbb11
./reshow "\(aaa\).*\(bbb\)" textfile must show
replone.c
./replone regexp replacement file prints file lines with regexp→replacement substitution.
No grouping operator in regexp (phiew! 😅)
Only first match is replaced (phiew again!)
- All possible errors must be treated accurately (i. e. check functions' return values)
H/W
Finish all tasks