我希望能够将一段句子分割成子字符串,在每个句子的结尾处,段落被分割。每句新句子都以玩家的名字开头。我的主要问题是在每个句子的开头为玩家的名字提供所有可能的格式。下面是一些示例:
投入:
Kentucky 1st - T. Pompey grounded out to 2b (2-0 BB). E. White lined out to cf (1-0 B). Z. Reks homered to right field, RBI (3-2 BFBBK). K. Cottam struck out swinging (1-2 KKBS).
UT Martin 2nd - KERWIN struck out looking (1-2 KSBK). ELDRIDGE fouled out to 3b (1-0 B). WESSLING walked (3-2 BKKBBK). STEEN reached on an error by ss (0-0); WESSLING advanced to second.
Belmont 2nd - EGLI, N. grounded out to 3b (0-1 K). BOURNIGAL, R walked (3-2 BSKBBB). KOCHER, B. walked (3-1 FBBBB); BOURNIGAL, R advanced to second. KOCHER, B. advanced to second; BOURNIGAL, R advanced to third on a throwing error by p.
Murray State 3rd - Bence, A. singled to left field (2-0 BB). Bence, A. stole second. Bauer, A. flied out to lf to left center (2-1 FBB); Bence, A. advanced to third. Gutzler, B. flied out to lf, SAC, RBI (1-0 B); Bence, A. scored. Hranec, J. fouled out to 1b (0-0). 期望产出:
'Kentucky 1st - '
'T. Pompey grounded out to 2b (2-0 BB).'
'E. White lined out to cf (1-0 B).'
'Z. Reks homered to right field, RBI (3-2 BFBBK).'
'K. Cottam struck out swinging (1-2 KKBS).'
'UT Martin 2nd - '
'KERWIN struck out looking (1-2 KSBK).'
'ELDRIDGE fouled out to 3b (1-0 B).'
'WESSLING walked (3-2 BKKBBK).'
'STEEN reached on an error by ss (0-0); WESSLING advanced to second.'
'Belmont 2nd - '
'EGLI, N. grounded out to 3b (0-1 K).'
'BOURNIGAL, R walked (3-2 BSKBBB).'
'KOCHER, B. walked (3-1 FBBBB); BOURNIGAL, R advanced to second.'
'KOCHER, B. advanced to second; BOURNIGAL, R advanced to third on a throwing error by p.'
'Murray State 3rd - '
'Bence, A. singled to left field (2-0 BB).'
'Bence, A. stole second.'
'Bauer, A. flied out to lf to left center (2-1 FBB); Bence, A. advanced to third.'
'Gutzler, B. flied out to lf, SAC, RBI (1-0 B); Bence, A. scored.'
'Hranec, J. fouled out to 1b (0-0).'我试图编写一个代码来提供以下输出(下面的代码中的“team”仅指开始这一段的团队-例如肯塔基州、UT Martin或贝尔蒙特):
s = re.split(r'[.][ ](?=[A-Z][^.])|[.][ ](?=[0-9])', s)
for i, x in enumerate(s):
x = x.split('.')[0]
if ' - ' in x:
team = '\n' + re.split(r'[0-9]', x.split(' - ')[0])[0].replace(';','').strip()
print(team + '\n')
y = x.split(' - ')[1] + '.'
if len(y)>2:
print(y + '\n')
else:
x = x + '.'
print(x + '\n')发布于 2017-07-20 05:20:20
如果您以不同的方式表述模式,那么很可能不必执行任何枚举:
s = re.split(r'([a-zA-Z0-9 ]+[^-] -|[A-Z][.][ ][^.]+|[A-Z]+[,.]*[ ]*[A-Z]*[,.]*[ ][^.]+)|.', s)
s = filter(None, s)
for v in s:
print(v.strip('\n\n'))结果
Kentucky 1st -
T. Pompey grounded out to 2b (2-0 BB)
E. White lined out to cf (1-0 B)
Z. Reks homered to right field, RBI (3-2 BFBBK)
K. Cottam struck out swinging (1-2 KKBS)
UT Martin 2nd -
KERWIN struck out looking (1-2 KSBK)
ELDRIDGE fouled out to 3b (1-0 B)
WESSLING walked (3-2 BKKBBK)
STEEN reached on an error by ss (0-0); WESSLING advanced to second
Belmont 2nd -
EGLI, N. grounded out to 3b (0-1 K)
BOURNIGAL, R walked (3-2 BSKBBB)
KOCHER, B. walked (3-1 FBBBB); BOURNIGAL, R advanced to second
KOCHER, B. advanced to second; BOURNIGAL, R advanced to third on a throwing error by p示例
https://stackoverflow.com/questions/45205211
复制相似问题