我试图从另一个字符串中提取一些子字符串,并且我已经确定了应该产生正确结果的模式,但是我认为我的实现中存在一些小缺陷。
s = 'Arkansas BaseballMiami (Ohio) at ArkansasFeb 17, 2017 at Fayetteville, Ark. (Baum Stadium)Score by Innings123456789RHEMiami (Ohio)000000000061Arkansas60000010X781Miami (Ohio) starters: 1/lf HALL, D.; 23/3b YACEK; 36/1b HAFFEY; 40/c SENGER; 7/dh HARRIS; 8/rf STEPHENS; 11/ss TEXIDOR; 2/2b VOGELGESANG; 5/cf SADA; 32/p GNETZ;Arkansas starters: 8/dh E. Cole; 9/ss J. Biggers; 17/lf L. Bonfield; 33/c G. Koch; 28/cf D. Fletcher; 20/2b C. Shaddy; 24/1b C Spanberger; 15/rf J. Arledge; 6/3b H. Wilson; 16/p B. Knight;Miami (Ohio) 1st - HALL, D. struck out swinging.'
这是我尝试使用正则表达式来实现我想要的输出:
teams = re.findall(r'(;|[0-9])(.*?) starters', s)
pitchers = re.findall('/p(.*?);', s)
投手搜索似乎有效,但是球队会输出以下内容:
[('1', '7, 2017 at Fayetteville, Ark. (Baum Stadium)Score by Innings123456789RHEMiami (Ohio)000000000061Arkansas60000010X781Miami (Ohio)'), ('1', '/lf HALL, D.; 23/3b YACEK; 36/1b HAFFEY; 40/c SENGER; 7/dh HARRIS; 8/rf STEPHENS; 11/ss TEXIDOR; 2/2b VOGELGESANG; 5/cf SADA; 32/p GNETZ;Arkansas')]
期望的输出:
['Miami (Ohio)', 'Arkansas']
[' GNETZ', ' B. Knight']
我可以担心稍后剥离投手名字中的前导空格。
答案 0 :(得分:1)
(;|[0-9])
可以替换为[;0-9]
。那么我认为你想要表达的是#34;在starters
之前和紧接在starters
"之前的最后一个数字/分号之后给我一个字符串,您可以说"中间不能有其他数字/分号",即
teams = re.findall(r'[;0-9]([^;0-9]*) starters', s)