import re
text = 'The first line leads off, With a gap before the next. Then the poem ends.'
print re.findall('.*?[,.!]?', text)
# ['The first line leads off,', ' With a gap before the next.', ' Then the poem ends.']
import re
mypoem = 'The first line leads off, With a gap before the next. Then the poem ends.'
junk = re.split("(,|\.)", mypoem)
poem_split = [i1 + i2 for i1, i2 in zip(junk[0::2], junk[1::2])]
只需使用正则表达式:
输出:
^{pr2}$或者您可以忘记为此编写自己的函数并使用重新分割还有拉链。重新分割将在使用捕获组时将结果列表中的分隔符保留为下一个元素。可以使用两个不同的步骤迭代和zip将其连接起来。在
相关问题 更多 >
编程相关推荐