Python中的非消耗正则表达式拆分 [英] Non-consuming regular expression split in Python

查看:26
本文介绍了Python中的非消耗正则表达式拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>text = "这是一个例子.它是由不止一个句子组成的吗?是的,是的.">>>re.split("[\.\?!] ", 文本)['这是一个例子','它是否由多个句子组成','是的,它是.']

我想要结果.

['这是一个例子.', '它由多个句子组成吗?', '是的.']

到目前为止,我只尝试了先行断言,但这根本无法拆分.

解决方案

import retext = "这是一个例子.一个特殊的案例.由更多的"\不止一次的句子?是的,是的.但没有空白!!!那是"\一个问题????是的.我想是的!:)"for x in re.split("(?<=[\.\?!]) ", text):打印代表(x)打印'\n'for x in re.findall("[^.?!]*[.?!]|[^.?!]+(?=\Z)",text):打印代表(x)

结果

这是一个例子.一个特殊的案例.由不止一次的句子组成?是的,它是.但没有空白!!!这是一个问题????是的.我想是的!"':)'这是一个例子."一个特殊的案例."由不止一次的句子组成?"'是的.'但没有空白!"'!'!这有问题?"'?'?'?'是的.''我认同!'':)'

.

编辑

还有

导入重新text = "!这是一个例子.一个特殊的案例.由更多的\不止一次的句子?是的,是的.但没有空白!!!那是"\一个问题????是的.我想是的!:)"res = re.split('([.?!])',text)打印 [ ''.join(res[i:i+2]) for i in xrange(0,len(res),2) ]

给予

['!', 'This is an example.', 'A special case.', '由多个句子组成?', 'Yes, it is.', '但没有空格!', '!', '!', "这是个问题吗?", '?', '?', '?', '是的.', '我也这么认为!', ' :)']

How can a string be split on a separator expression while leaving that separator on the preceding string?

>>> 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.']

I would like the result to be.

['This is an example.', 'Is it made up of more than one sentence?', 'Yes, it is.']

So far I have only tried a lookahead assertion but this fails to split at all.

解决方案

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)

result

"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!'
' :)'

.

EDIT

Also

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) ]

gives

['!', ' 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!', ' :)']

这篇关于Python中的非消耗正则表达式拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆