我有一个凌乱的字符串列表(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
我将使用
rstrip()
和split()
功能使用不同的方法处理正则表达式:我会在这里使用列表:
用这种方式编写代码。在这里修复OP的尝试本身。您的正则表达式正在做所有的事情,唯一的一点就是添加一个OR条件,在这里我们可以提到替换1次或多次出现的
]
相关问题 更多 >
编程相关推荐