这应该是使用该re
库的一项非常简单的任务。但是,我似乎无法在分隔符]
和[
.
我已经阅读了在 Python 中使用多个分隔符拆分字符串、Python:使用多个分隔符拆分字符串和Python:如何在方括号内获取多个元素。
我的字符串:
data = "This is a string spanning over multiple lines.
At somepoint there will be square brackets.
[like this]
And then maybe some more text.
[And another text in square brackets]"
它应该返回:
['This is a string spanning over multiple lines.\nAt somepoint there will be square brackets.','like this', 'And then maybe some more text.', 'And another text in square brackets']
一个简短的例子:
data2 = 'A new string. [with brackets] another line [and a bracket]'
我试过了:
re.split(r'(\[|\])', data2)
re.split(r'([|])', data2)
但这些会导致在我的结果列表中包含分隔符或完全错误的列表:
['A new string. ', '[', 'with brackets', ']', ' another line ', '[', 'and a bracket', ']', '']
结果应该是:
['A new string.', 'with brackets', 'another line', 'and a bracket']
作为特殊要求,应删除分隔符前后的所有换行符和空格,也不应包含在列表中。