Python中的非消耗正则表达式拆分

Rob*_*ung 10 python regex

如何在分隔符表达式上拆分字符串,同时将该分隔符留在前面的字符串上?

>>> text = "This is an example. Is it made up of more than once sentence? Yes, it is."
>>> re.split("[\.\?!] ", text)
['This is an example', 'Is it made up of more than one sentence', 'Yes, it is.']
Run Code Online (Sandbox Code Playgroud)

我希望结果如此.

['This is an example.', 'Is it made up of more than one sentence?', 'Yes, it is.']
Run Code Online (Sandbox Code Playgroud)

到目前为止,我只尝试了一个先行断言,但这根本没有分裂.

Ign*_*ams 11

>>> re.split("(?<=[\.\?!]) ", text)
['This is an example.', 'Is it made up of more than once sentence?', 'Yes, it is.']
Run Code Online (Sandbox Code Playgroud)

关键是使用后断言?<=.

  • @all @ThomasH 到目前为止,我引起了十位投票者的注意,他们投票了一个错误的答案:这个答案中使用的分隔符是空格,并且这个空格没有保留在每个分隔的字符串中,这与关键要求相反OP的。ThomasH,我提请您注意这样一个事实:即使答案很简洁,如果它是错误的,也不会是更好的答案。 (2认同)

eyq*_*uem 9

import re

text = "This is an example.A particular case.Made up of more "\
       "than once sentence?Yes, it is.But no blank !!!That's"\
       " a problem ????Yes.I think so! :)"


for x in re.split("(?<=[\.\?!]) ", text):
    print repr(x)

print '\n'

for x in re.findall("[^.?!]*[.?!]|[^.?!]+(?=\Z)",text):
    print repr(x)
Run Code Online (Sandbox Code Playgroud)

结果

"This is an example.A particular case.Made up of more than once sentence?Yes, it is.But no blank !!!That'sa problem ????Yes.I think so!"
':)'


'This is an example.'
'A particular case.'
'Made up of more than once sentence?'
'Yes, it is.'
'But no blank !'
'!'
'!'
"That's a problem ?"
'?'
'?'
'?'
'Yes.'
'I think so!'
' :)'
Run Code Online (Sandbox Code Playgroud)

.

编辑

import re

text = "! This is an example.A particular case.Made up of more "\
       "than once sentence?Yes, it is.But no blank !!!That's"\
       " a problem ????Yes.I think so! :)"

res = re.split('([.?!])',text)

print [ ''.join(res[i:i+2]) for i in xrange(0,len(res),2) ]
Run Code Online (Sandbox Code Playgroud)

['!', ' This is an example.', 'A particular case.', 'Made up of more than once sentence?', 'Yes, it is.', 'But no blank !', '!', '!', "That's a problem ?", '?', '?', '?', 'Yes.', 'I think so!', ' :)']
Run Code Online (Sandbox Code Playgroud)