1
我的模板引擎转换正则表达式:匹配报价,那是不能取代
"some data #{substitution_expression} some other data"
到
"some data" + (substitution_expression) + "some other data"
但是,如果“一些数据”或“其他一些数据”必须有双引号,评估失败。 我必须在这些引号前添加斜杠,但我无法为此提供正确的正则表达式。
任何帮助?
UPDATE:
这是模板引擎是如何工作的:
它得到一个模板字符串,例如
template = 'template string "quoted text" #{expression}'
它通过一个简单的正则表达式改变模板字符串:
template = '"%s"' % re.compile(r'\#{(.*)}').match(r'" + (\1) + "', template) # template == "template string "quoted text"" + (expression) + "" # here is a problem with a "quoted text" - it needs \ before quotes`
此字符串被插入到拉姆达,并将结果码串被evaled:
return eval("lambda tpl_args: %s" % modified_template_string)
稍后在程序中调用lambda,并使用一些tpl_args生成结果字符串。
我不明白这个问题。你能描述一下你所描述的是什么行不通?你有什么字符串,你想匹配什么? –
您应该多介绍一下模板引擎的工作原理。如果可能的话,或许向我们展示更多代码 –
你现在使用的是什么代码? – NullUserException