This should be an really easy task using the re
library. However, I can't seem to split my string at the delimiters ]
and [
.
I already read Splitting a string with multiple delimiters in Python, Python: Split string with multiple delimiters, and Python: How to get multiple elements inside square brackets.
My string:
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]"
It should return:
['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']
A short example to try:
data2 = 'A new string. [with brackets] another line [and a bracket]'
I tried:
re.split(r'(\[|\])', data2)
re.split(r'([|])', data2)
But those would either result in having the delimiter in my resulting list or a wrong list altogether:
['A new string. ', '[', 'with brackets', ']', ' another line ', '[', 'and a bracket', ']', '']
Result should be:
['A new string.', 'with brackets', 'another line', 'and a bracket']
As a special requirement all newline characters and white spaces before and after a delimiter should be removed and not be included in the list either.