Improve template conditionals parsing
The old method with big regexps was buggy as described in #8065.