Python 2.7:如何从特殊字符之间提取

Python 2.7 : how to extract from between special characters

我有这样的字符串:

'|||stuff||things|||others||||'

有没有一种简单的方法可以提取|之间包含的所有内容?字符,因此最终结果将类似于:

result = ['stuff', 'things', 'others']

编辑:我不会事先知道字符串的内容,因为我不知道具体要查找 'stuff'、'things' 或 'others',我只是知道 | 之间的任何东西字符需要另存为单独的字符串

拆分一个或多个 |re.split:

re.split(r'\|+', str_)

这将在开始和结束处给出空字符串,以摆脱那些使用列表推导仅采用 truthy:

的字符串
[i for i in re.split(r'\|+', str_) if i]

示例:

In [193]: str_ = '|||stuff||things|||others||||'

In [194]: re.split(r'\|+', str_)
Out[194]: ['', 'stuff', 'things', 'others', '']

In [195]: [i for i in re.split(r'\|+', str_) if i]
Out[195]: ['stuff', 'things', 'others']
[i for i in string.split('|') if i!='']

这应该有效