我試圖建立一個函數來驗證一個字符串不包含大括號({
和}
),除了那些可能用於一個級別的格式。也就是說,我希望允許大括號在對字符串的.format
方法進行一次調用後消失。驗證一個字符串不包含除用於格式化的那些大括號
例如,如果該驗證函數被調用no_curly_braces
,下面的結果應返回:
>>> no_curly_brackets("word")
True
>>> no_curly_brackets("{word") # False, as .format raises ValueError
False
>>> no_curly_brackets("{}word") # True, as .format(3) returns '3word'
True
>>> no_curly_brackets("{word}") # True, as .format(word=3) returns '3'
True
>>> no_curly_brackets("{{word}}") # False, as .format returns '{word}'
False
>>> no_curly_brackets("{{{word}}}") # False, as .format(word='a') returns '{a}'
False
>>> no_curly_brackets("{word}{{}}") # False, as .format(word=3) returns '3{}'
False
等。
我的問題是,像"{" in str
嘗試將失敗(如模板可以持有這些大括號),我也不想不知道什麼,我應該向.format
方法格式化,以儘量使相關的大括號消失。
XY問題? http://www.perlmonks.org/?node_id=542341 –
@MartijnPieters:問題是這樣的:無論你如何格式化{{word}}或'{{{word}}}',在一次調用'.format'後會留下大括號。 – Bach
@ColonelPanic:這主要是爲了純粹的好奇心問的。 – Bach