我正在使用正则表达式以多个分隔符分割字符串。但是,如果我的两个分隔符在字符串中相邻,它会在结果列表中放置一个空字符串。例如:re.split(',|;', "This,is;a,;string") 执行结果为['This', 'is', 'a', '', 'string'] 有没有什么方法可以避免在列表中获取'',而不必添加,作为分隔符?
re.split(',|;|string', "This,is;a,;string")
- David DeMarre.split(r'[,;]+|string', 'This,is;a,;string')
- Óscar López['This', 'is', 'a', '', '']
。 - David DeMar[s for s in re.split(r'[,;]+|string', 'This,is;a,;string') if s]
- Óscar López