my ($rx) = map { length() ? qr{ ([\Q$_\E]) .*? \1 }xms : qr/(?!)/ } join '', split '', $template ; return $rx;