本文介绍了在不删除分隔符的情况下拆分正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我想把这段文字分成句子.
So, I would like to split this text into sentences.
s = "You! Are you Tom? I am Danny."
所以我得到:
["You!", "Are you Tom?", "I am Danny."]
也就是说,我想通过正则表达式 '[.!\?]'
拆分文本而不删除分隔符.在python中实现这一点的最pythonic方法是什么?
That is I want to split the text by the regex '[.!\?]'
without removing the delimiters. What is the most pythonic way to achieve this in python?
我知道这些问题:
但我的问题有各种分隔符(.?!
),这使问题复杂化.
But my problem has various delimiters (.?!
) which complicates the problem.
推荐答案
您可以使用 re.findall
和正则表达式 .*?[.!\?]
;惰性量词 *?
确保每个模式匹配到您想要匹配的特定分隔符:
You can use re.findall
with regex .*?[.!\?]
; the lazy quantifier *?
makes sure each pattern matches up to the specific delimiter you want to match on:
import re
s = """You! Are you Tom? I am Danny."""
re.findall('.*?[.!\?]', s)
# ['You!', ' Are you Tom?', ' I am Danny.']
这篇关于在不删除分隔符的情况下拆分正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!