简体   繁体   English

拆分文本,但使用正则表达式将分隔符保留在句末

[英]split text but keep the delimiter at end of sentence with regex

I have a text like "Hi.My name is jeff?How are you?fine" .我有一个类似"Hi.My name is jeff?How are you?fine"这样的文字。

I want to split this text based on .我想根据. and ?? with regex to get output like this:用正则表达式得到 output 像这样:

['Hi.', 'My name is jeff!How are you?', 'fine']

I tried $ and also \Z , but it did not work.我试过$\Z ,但它没有用。

Use利用

import re
string = "Hi.My name is jeff!How are you?fine"
print(re.split(r"(?<=[.?])", string))

See Python proof .参见Python 证明 Also, see regex proof .另外,请参阅正则表达式证明

EXPLANATION解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    [.?]                     any character of: '.', '?'
--------------------------------------------------------------------------------
  )                        end of look-behind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM