首页 / 正则 / Python中的非使用正则表达式拆分
Python中的非使用正则表达式拆分
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python中的非使用正则表达式拆分,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1526字,纯文字阅读大概需要3分钟。
内容图文
如何在分隔符表达式上拆分字符串,同时将该分隔符保留在前面的字符串上?
>>> 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.']
我希望结果是.
['This is an example.', 'Is it made up of more than one sentence?', 'Yes, it is.']
到目前为止,我仅尝试了前瞻性断言,但这根本无法解决.
解决方法:
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)
结果
"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!'
' :)'
.
编辑
也
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) ]
给
['!', ' 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中的非使用正则表达式拆分全部内容,希望文章能够帮你解决Python中的非使用正则表达式拆分所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。