(a) Octal anywhere (write ONLY the RE, never
regex(...)):[0-7]+
(b) Octal NOT touching letters or digits (watch start/end of line!):
(^|[^A-Za-z0-9])[0-7]+([^A-Za-z0-9]|$)
(c) C Expression (1-deep parens — name pieces on paper!):
// Build & name each piece on your exam paper: operand = ([0-9]+|[A-Za-z_][A-Za-z0-9_]*) paren_grp = \( *operand( *[-+*/] *operand)* *\) term = (operand|paren_grp) expression = term( *[-+*/] *term)*
(d) C Loop (
regcomp / regexec advancing via rm_eo):regex_t re; regmatch_t m[1]; if (regcomp(&re, PATTERN, REG_EXTENDED) != 0) { printf("bad RE\n"); exit(1); } FILE *f = fopen("this-is-very-annoying.c", "r"); if (f == NULL) { perror("open"); exit(1); } char line[1000]; while (fgets(line, 1000, f) != NULL) { char *p = line; while (regexec(&re, p, 1, m, 0) == 0) { printf("%.*s\n", m[0].rm_eo - m[0].rm_so, p + m[0].rm_so); p += m[0].rm_eo; // advance past match! } } fclose(f); regfree(&re);
TRAP: Always pass
REG_EXTENDED so +, |, and (...) work without backslashes.