[英]Split string by multiple delimiters, ignore repeating delimiters
I'm trying to parse a string that has multiple delimiters which may be repeating.我正在尝试解析具有多个可能重复的分隔符的字符串。
Input string: "-abc,-def,ghi-jkl,mno"输入字符串:“-abc,-def,ghi-jkl,mno”
Expected return: ["abc", "def", "ghi", "jkl", "mno"]预期回报:["abc", "def", "ghi", "jkl", "mno"]
I've tried我试过了
re.split(",|-", string)
But the return is:但回报是:
['', 'abc', '', 'def', 'ghi', 'jkl', 'mno']
You can filter the result like this您可以像这样过滤结果
>>> list(filter(len, re.split(r"[,|-]+", s)))
['abc', 'def', 'ghi', 'jkl', 'mno']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.