Fixing a regexp bug in template matching and matching new templates