format template and replace regex code with substr