Python:如何在字符串拆分中包含分隔符?

时间:2014-01-18 18:36:06

标签: python string parsing split

我想拆分一个包含多个分隔符的字符串,但是在结果列表中保留分隔符。我认为这是解析任何类型公式的初始步骤是有用的,我怀疑有一个很好的Python解决方案。

有人在Java here中提出了类似的问题。

例如,典型的拆分如下所示:

>>> s='(twoplusthree)plusfour'
>>> s.split(f, 'plus')
['(two', 'three)', 'four']

但我正在寻找一种很好的方法来添加加回(或保留它):

['(two', 'plus', 'three)', 'plus', 'four']

最终我想为每个操作员和支架做这个,所以如果有办法可以获得

['(', 'two', 'plus', 'three', ')', 'plus', 'four']

一气呵成,然后更好。

4 个答案:

答案 0 :(得分:13)

您可以使用Python的re模块。

import re
s='(twoplusthree)plusfour'
list(filter(None, re.split(r"(plus|[()])", s)))

如果只需要迭代器,则可以省略列表。

答案 1 :(得分:4)

import re
s = '(twoplusthree)plusfour'
l = re.split(r"(plus|\(|\))", s)
a = [x for x in l if x != '']
print a

输出:

['(', 'two', 'plus', 'three', ')', 'plus', 'four']

答案 2 :(得分:4)

使用re.split

这是一种简单的方法
import re

s = '(twoplusthree)plusfour'
re.split('(plus)',  s)

<强>输出:

['(two', 'plus', 'three)', 'plus', 'four']

re.splitstring.split非常相似,除了您传递正则表达式模式而不是文字分隔符。这里的技巧是在模式周围放置(),以便将其作为一组提取。

请记住,如果分隔符模式连续两次出现,您将拥有空字符串

答案 3 :(得分:0)

在这里,我是在首次出现字母字符时分割字符串:

def split_on_first_alpha(i):
    #i="3.5 This is one of the way"
    split_1=re.split(r'[a-z]',i,maxsplit=1, flags=re.IGNORECASE)
    find_starting=re.findall(r'[a-z]',i,flags=re.IGNORECASE)
    split_1[1]=find_starting[0]+split_1[1]
    return split_1
相关问题