我有一个示例字符串<alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card] ...>, created=1324336085, description='Customer for My Test App', livemode=False>
我只需要值cus_Y4o9qMEZAugtnW
,而不需要card
(它在另一个[]
中)
我如何在Python中以最简单的方式做到这一点呢?也许可以使用RegEx (我并不擅长)?
发布于 2011-12-20 08:07:16
你可以使用str.split
来做这件事。
s = "<alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card]\
...>, created=1324336085, description='Customer for My Test App',\
livemode=False>"
val = s.split('[', 1)[1].split(']')[0]
然后我们就有了:
>>> val
'cus_Y4o9qMEZAugtnW'
发布于 2011-12-20 08:09:13
这应该可以完成这项工作:
re.match(r"[^[]*\[([^]]*)\]", yourstring).groups()[0]
发布于 2016-08-10 19:14:17
your_string = "lnfgbdgfi343456dsfidf[my data] ljfbgns47647jfbgfjbgskj"
your_string[your_string.find("[")+1 : your_string.find("]")]
https://stackoverflow.com/questions/8569201
复制相似问题