Regex 如何在包含标点符号的同时将字符串拆分成句子?

Regex 如何在包含标点符号的同时将字符串拆分成句子?,regex,python-3.x,string,punctuation,sentence,Regex,Python 3.x,String,Punctuation,Sentence,我希望分割的句子包括标点符号(例如:?,!,),如果句子末尾有双引号,我也希望包括它 我使用python3中的re.split()函数将字符串拆分成句子。但遗憾的是,结果字符串不包括标点符号,如果句子末尾有双引号,也不包括双引号 这就是我当前的代码: x = 'This is an example sentence. I want to include punctuation! What is wrong with my code? It makes me want to yell, "PLEA

我希望分割的句子包括标点符号(例如:?,!,),如果句子末尾有双引号,我也希望包括它

我使用python3中的re.split()函数将字符串拆分成句子。但遗憾的是,结果字符串不包括标点符号,如果句子末尾有双引号,也不包括双引号

这就是我当前的代码:

x = 'This is an example sentence. I want to include punctuation! What is wrong with my code? It makes me want to yell, "PLEASE HELP ME!"'
sentence = re.split('[\.\?\!]\s*', x)
我得到的结果是:

['This is an example sentence', 'I want to include punctuation', 'What is wrong with my code', 'It makes me want to yell, "PLEASE HELP ME', '"']

尝试在查找时拆分:

sentences = re.split('(?<=[\.\?\!])\s*', x)
print(sentences)

['This is an example sentence.', 'I want to include punctuation!',
 'What is wrong with my code?', 'It makes me want to yell, "PLEASE HELP ME!"']

(对不起,我在“re”前面有一个括号因此,您的代码中也有它。请将其编辑掉。至于结果,除了字符串末尾的双引号外,所有内容都有效。您运行了您的吗?它对您有效吗?对我来说,引号在结果列表中显示为单独的元素。@investigate311。我们可以调整我的答案以处理双引号。updated双引号版本适合我!我用循环清除了无和空字符串。
x = 'This is an example sentence. I want to include punctuation! "What is wrong with my code?"  It makes me want to yell, "PLEASE HELP ME!"'
sentences = re.split('((?<=[.?!]")|((?<=[.?!])(?!")))\s*', x)
print filter(None, sentences)

['This is an example sentence.', 'I want to include punctuation!',
 '"What is wrong with my code?"', 'It makes me want to yell, "PLEASE HELP ME!"']