簡體   English   中英

拆分文本,但使用正則表達式將分隔符保留在句末

[英]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))

參見Python 證明 另外,請參閱正則表達式證明

解釋

--------------------------------------------------------------------------------
  (?<=                     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