Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用正则表达式使用多个分隔符拆分字符串。但是,如果我的两个分隔符在字符串中相邻出现,则会在结果列表中放置一个空字符串。例如:
re.split(',|;', "This,is;a,;string")
结果是
['This', 'is', 'a', '', 'string']
有没有办法避免''在不添加,;分隔符的情况下进入我的列表?
''
,;
试试这个:
import re re.split(r'[,;]+', 'This,is;a,;string') > ['This', 'is', 'a', 'string']