【发布时间】:2021-07-17 11:06:31
【问题描述】:
我有一个混乱的字符串列表 (list_strings),我可以使用 regex 删除不需要的字符,但我正在努力删除右括号 ] 。我怎样才能删除那些?我想我很接近了……
#the list to clean
list_strings = ['[ABC1: text1]', '[[DC: this is a text]]', '[ABC-O: potatoes]', '[[C-DF: hello]]']
#remove from [ up to :
for string in list_strings:
cleaned = re.sub(r'[\[A-Z\d\-]+:\s*', '', string)
print(cleaned)
# current output
>>>text1]
>>>this is a text]]
>>>potatoes]
>>>hello]
期望的输出:
text1
this is a text
potatoes
hello
【问题讨论】: