If you use re.split
, then a delimiter at the beginning or end of the string causes an empty string at the beginning or end of the array in the result.
If you don't want this, use re.findall
with a regex that matches every sequence NOT containing delimiters.
Example:
import re
a = '[1 2 3 4]'
print(re.split(r'[^d]+', a))
print(re.findall(r'[d]+', a))
Output:
['', '1', '2', '3', '4', '']
['1', '2', '3', '4']
As others have pointed out in their answers, this may not be the perfect solution for this problem, but it is a general answer to the problem described in the title of the question, which I also had to solve when I found this question using Google.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…