将句子分成单独的字符串,句子以大写字母开头

时间:2017-06-22 02:33:43

标签: python regex

基本上,我想将以下字符串拆分为两个单独的字符串,例如:

输入: ' LIPCIUS,A。接地到3b(1-2 FBF); AMMONS进入第二名。 MOBERG摆脱了摇摆(2-2 BSSFBS)。'

输出: [' LIPCIUS,A。接地到3b(1-2 FBF); AMMONS进入第二名。', ' MOBERG击败(2-2 BSSFBS)。']

新的句子是我的情况,总是以大写字母(即玩家的名字)开头。以下是我尝试执行此操作的代码:

HistoryGrid.Items.SortDescriptions

我的代码当前输出以下内容,列表中的第一个字符串不准确:

import re

string = 'LIPCIUS, A. grounded out to 3b (1-2 FBF); AMMONS advanced to second. MOBERG struck out swinging (2-2 BSSFBS).'
x = re.findall("[A-Z].*?[\.!?]", string, re.DOTALL)
print(x)

2 个答案:

答案 0 :(得分:1)

下面的正则表达式应该适合您,添加大写字母的可选lookahead assertion$后跟.,以避免在A.B. <停留/ p>

import re
string = 'LIPCIUS, A. grounded out to 3b (1-2 FBF); AMMONS advanced to second. MOBERG struck out swinging (2-2 BSSFBS).'
x = re.findall("[A-Z].*?[\.!?]\s?(?=[A-Z]|$)", string, re.DOTALL)
# ['LIPCIUS, A. grounded out to 3b (1-2 FBF); AMMONS advanced to second. ', 'MOBERG struck out swinging (2-2 BSSFBS).']

答案 1 :(得分:1)

import re
s = 'LIPCIUS, A. grounded out to 3b (1-2 FBF); AMMONS advanced to second. MOBERG struck out swinging (2-2 BSSFBS).'
l = re.split(r'[.][ ](?=[A-Z]+\b)', s)
print l

它只是在每个想要的输出项目之后不包括点,但我想它不会打扰你。

相关问题