例如,如果我有一个模板,如
"SELECT * FROM myOrders WHERE order_id = ${orderId} and order_date = ${orderDate}"
我想获得该模板中所有占位符的列表,即[' orderId',' orderDate']
答案 0 :(得分:1)
我可以使用正则表达式来拉出这些占位符。
P.S。尽管如此,在Cheetah API中使用该功能会很好,也许在Template类中。
示例:
import re
templateDef = """
SELECT * FROM myOrders WHERE order_id = ${orderId} and order_date = ${orderDate}
"""
placeholders = re.findall(r"\${(\w+)}", templateDef)
print placeholders
答案 1 :(得分:1)
Formatter.parse应该有效:
s = "SELECT * FROM myOrders WHERE order_id = ${orderId} and order_date = ${orderDate}"
from string import Formatter
#literal_text, field_name, format_spec, conversion
print([plh for _, plh, _,_ in Formatter().parse(s) if plh])
['orderId', 'orderDate']
答案 2 :(得分:0)
此代码:
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
打印:
{'B','A','C'}