alv*_*vas 0 c python regex string string-formatting
我可以这样格式化一个字符串str.format(**arg):
>>> a, b, c = 1123,242,32364
>>> "{}_{}_{}".format(a,b,c)
'1123_242_32364'
但我可以反过来检查字符串是否符合某种格式?例如
>>> "{}_{}_{}".check_format("a_bc_def")
True
>>> a,b,c = "{}_{}_{}".deformat("a_bc_def")
>>> a
a
>>> b
bc
>>> c
def
>>> "{}_{}_{}".chcek_format("_____")
True
>>> a,b,c = "{}_{}_{}".deformat("_____")
>>> a == b == c == "_"
True
>>> "{}_{}_{}".chcek_format("_1ad_das__")
True
>>> a,b,c = "{}_{}_{}".deformat("_1ad_das__")
>>> a
_1ad
>>> b
das
>>> c
_
Run Code Online (Sandbox Code Playgroud)
有一个解析模块,被描述为"格式()的反面".我用它安装后pip:
>>> from parse import *
>>> parse("{}_{}_{}", "a_bc_def")
<Result ('a', 'bc', 'def') {}>
>>> a,b,c = parse("{}_{}_{}", "_1ad_das__")
>>> a
'_1ad'
>>> b
'das'
>>> c
'_'
Run Code Online (Sandbox Code Playgroud)