Remove obsolete imports.
Self-tests for regex matching.

Also tighten exception handling around file opening.