【发布时间】:2021-03-16 20:51:08
【问题描述】:
我有一个类似"Hi.My name is jeff!How are you?fine" 的文本。
我想根据. 和? 使用正则表达式拆分此文本以获得如下输出:
['Hi.', 'My name is jeff!How are you?', 'fine']
我尝试过$ 和\Z,但没有成功。
【问题讨论】:
标签: python python-3.x regex
我有一个类似"Hi.My name is jeff!How are you?fine" 的文本。
我想根据. 和? 使用正则表达式拆分此文本以获得如下输出:
['Hi.', 'My name is jeff!How are you?', 'fine']
我尝试过$ 和\Z,但没有成功。
【问题讨论】:
标签: python python-3.x regex
使用
import re
string = "Hi.My name is jeff!How are you?fine"
print(re.split(r"(?<=[.?])", string))
见Python proof。另请参阅regex proof。
解释
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
[.?] any character of: '.', '?'
--------------------------------------------------------------------------------
) end of look-behind
【讨论】: