[英]split text but keep the delimiter at end of sentence with regex
我有一個類似"Hi.My name is jeff?How are you?fine"
這樣的文字。
我想根據.
和?
用正則表達式得到 output 像這樣:
['Hi.', 'My name is jeff!How are you?', 'fine']
我試過$
和\Z
,但它沒有用。
利用
import re
string = "Hi.My name is jeff!How are you?fine"
print(re.split(r"(?<=[.?])", string))
解釋
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
[.?] any character of: '.', '?'
--------------------------------------------------------------------------------
) end of look-behind
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.