我的文字如下(底部)。这是足球的比赛数据。我正在根据本季度的两个参数,季度和分钟来创建一个函数来从这个文本中获取数据。因此,如果函数被传递quarter=q2, time=10
,我必须从头开始抓取所有文本,直到我在第二季度记录10分钟时播放的游戏。问题是第二季度可能没有时间戳可以找到10分钟。所以正则表达式会一直持续到第三季度或第四季度...另一个问题是,如果第二季度没有时间戳10,那么正确的行为是抓住所有文本,直到时间戳为9,然后是8,等等
我可以通过将游戏数据分成四个文本变量来实现这一点,每个季度一个(第四季度将包括加班)。然后我可以制作一个正则表达式,在正确的时间找到给定四分之一的游戏,并将其粘贴在一个循环中,这将继续调整时间戳直到匹配。然后我可以将所有前几个季度的文本(例如,如果我在第2季的文本变量中搜索,只是第1季度)附加到我在指定季度找到的匹配项。
有没有更好的方法来实现这一点,“高效”,正则表达式?
1st Quarter Play by Play Marshall at 15:00 MRSH MEM
Anthony Binswanger kickoff for 63 yards returned by Michael Grandberry for 23 yards to the Memph 30.
Memphis at 14:45 MRSH MEM
1st and 10 at MEM 30 Earnest Williams rush for 12 yards to the Memph 42 for a 1ST down.
1st and 10 at MEM 42 Will Hudgens pass complete to Duke Calhoun for 30 yards to the Marsh 28 for a 1ST down.
1st and 10 at MRSH 28 Will Hudgens pass complete to Brett Russell for 16 yards to the Marsh 12 for a 1ST down out-of-bounds.
1st and 10 at MRSH 12 Carlton Robinzine rush for 1 yard to the Marsh 11 out-of-bounds.
2nd and 9 at MRSH 11 Will Hudgens pass complete to Maurice Jones for no gain to the Marsh 11.
3rd and 9 at MRSH 11 Will Hudgens pass complete to Duke Calhoun for 3 yards to the Marsh 8.
4th and 6 at MRSH 8 Matt Reagan 25 yard field goal GOOD.
Matt Reagan kickoff for 64 yards returned by Darius Marshall for 8 yards to the Marsh 14.
DRIVE TOTALS: Memph drive: 7 plays 62 yards, 02:12 Memph FG
Marshall at 12:48 MRSH MEM
1st and 10 at MRSH 14 Darius Marshall rush for 1 yard to the Marsh 15.
2nd and 9 at MRSH 15 Bernard Morris pass complete to Emmanuel Spann for 6 yards to the Marsh 21.
3rd and 3 at MRSH 21 Bernard Morris pass incomplete to Darius Passmore
4th and 3 at MRSH 21 Anthony Binswanger punt for 23 yards downed at the Marsh 44.
DRIVE TOTALS: Marsh drive: 3 plays 7 yards, 01:37 Marsh PUNT
Memphis at 11:11 MRSH MEM
1st and 10 at MRSH 44 Will Hudgens pass incomplete to Steven Black
2nd and 10 at MRSH 44 Joseph Doss rush for 6 yards to the Marsh 38.
3rd and 4 at MRSH 38 Will Hudgens pass complete to Earnest Williams for 15 yards to the Marsh 23 for a 1ST down.
1st and 10 at MRSH 23 Joseph Doss rush for 3 yards to the Marsh 20.
2nd and 7 at MRSH 20 Matt Malouf rush for 1 yard to the Marsh 19 out-of-bounds.
3rd and 6 at MRSH 19 Will Hudgens pass complete to Brett Russell for 12 yards to the Marsh 7 for a 1ST down.
1st and Goal at MRSH 7 Memphis penalty 5 yard false start on Brandon Pearce accepted.
1st and Goal at MRSH 12 Will Hudgens rush for a loss of 6 yards to the Marsh 18.
2nd and Goal at MRSH 18 Will Hudgens pass incomplete to Brett Russell
3rd and Goal at MRSH 18 Will Hudgens pass complete to Joseph Doss for 9 yards to the Marsh 9.
4th and Goal at MRSH 9 Matt Reagan 26 yard field goal GOOD.
Matt Reagan kickoff for 64 yards returned by Darius Marshall for 17 yards to the Marsh 23.
DRIVE TOTALS: Memph drive: 10 plays 35 yards, 03:43 Memph FG
Marshall at 7:19 MRSH MEM
1st and 10 at MRSH 23 Darius Marshall rush for 1 yard to the Marsh 24.
2nd and 9 at MRSH 24 Bernard Morris pass complete to Darius Passmore for 8 yards to the Marsh 32.
3rd and 1 at MRSH 32 Darius Marshall rush for 2 yards to the Marsh 34 for a 1ST down.
1st and 10 at MRSH 34 Memphis penalty 5 yard offside accepted, no play.
1st and 5 at MRSH 39 Bernard Morris sacked by Jada Brown at the Marsh 38 for a loss of 1 yard.
2nd and 6 at MRSH 38 Bernard Morris rush for 2 yards to the Marsh 40.
3rd and 4 at MRSH 40 Bernard Morris pass intercepted by LeRico Mathis at the Memph 44, returned for 2 yards to the Memph 46.
DRIVE TOTALS: Marsh drive: 6 plays 17 yards, 03:44 Marsh INT
Memphis at 4:13 MRSH MEM
1st and 10 at MEM 46 T.J. Pitts rush for 3 yards to the Memph 49.
2nd and 7 at MEM 49 Will Hudgens pass complete to Duke Calhoun for 10 yards to the Marsh 41 for a 1ST down out-of-bounds.
1st and 10 at MRSH 41 T.J. Pitts rush for 4 yards to the Marsh 37.
2nd and 6 at MRSH 37 Will Hudgens pass incomplete to Carlos Singleton
3rd and 6 at MRSH 37 Will Hudgens pass incomplete to Carlos Singleton
4th and 6 at MRSH 37 Matt Malouf punt for 31 yards downed at the Marsh 6.
DRIVE TOTALS: Memph drive: 5 plays 17 yards, 01:36 Memph PUNT
Marshall at 2:08 MRSH MEM
1st and 10 at MRSH 6 Darius Marshall rush for 9 yards to the Marsh 15 out-of-bounds.
2nd and 1 at MRSH 15 Darius Marshall rush for 7 yards to the Marsh 22 for a 1ST down.
1st and 10 at MRSH 22 Chubb Small rush for 9 yards to the Marsh 31.
2nd and 1 at MRSH 31 Chubb Small rush for 8 yards to the Marsh 39 for a 1ST down.
1st and 10 at MRSH 39 Darius Marshall rush for 3 yards to the Marsh 42.
End of 1st Quarter
2nd Quarter Play by Play Marshall at 15:00 MRSH MEM
2nd and 7 at MRSH 42 Darius Marshall rush for 1 yard to the Marsh 43.
3rd and 6 at MRSH 43 Bernard Morris rush for 4 yards to the Marsh 47.
4th and 2 at MRSH 47 Anthony Binswanger punt for 40 yards, returned by Earnest Williams for 4 yards to the Memph 17.
DRIVE TOTALS: Marsh drive: 7 plays 41 yards, 03:00 Marsh PUNT
Memphis at 14:08 MRSH MEM
1st and 10 at MEM 17 T.J. Pitts rush for 4 yards to the Memph 21.
2nd and 6 at MEM 21 T.J. Pitts rush for 6 yards to the Memph 27 for a 1ST down.
1st and 10 at MEM 27 T.J. Pitts rush for 4 yards to the Memph 31.
2nd and 6 at MEM 31 Will Hudgens pass complete to Maurice Jones for 7 yards to the Memph 38 for a 1ST down.
1st and 10 at MEM 38 T.J. Pitts rush for 1 yard to the Memph 39.
2nd and 9 at MEM 39 Joseph Doss rush for a loss of 1 yard to the Memph 38.
3rd and 10 at MEM 38 Will Hudgens pass incomplete to Duke Calhoun
4th and 10 at MEM 38 Brent Sutherland punt for 41 yards, returned by Bryant Milligan for 13 yards to the Marsh 34.
DRIVE TOTALS: Memph drive: 7 plays 21 yards, 03:47 Memph PUNT
Marshall at 10:21 MRSH MEM
1st and 10 at MRSH 34 Bernard Morris pass complete to Emmanuel Spann for 25 yards to the Memph 41 for a 1ST down.
1st and 10 at MEM 41 Bernard Morris pass complete to Darius Marshall for 7 yards to the Memph 34.
2nd and 3 at MEM 34 Bernard Morris pass complete to Cody Slate for 4 yards to the Memph 30 for a 1ST down.
1st and 10 at MEM 30 Bernard Morris pass complete to Chubb Small for no gain to the Memph 30.
2nd and 10 at MEM 30 Bernard Morris rush for 5 yards to the Memph 25.
3rd and 5 at MEM 25 Memphis penalty 5 yard offside on Freddie Barnett accepted, no play.
1st and 10 at MEM 20 Timeout MARSHALL, clock 06:57.
1st and 10 at MEM 20 Bernard Morris pass complete to Cody Slate for 20 yards for a TOUCHDOWN.
Anthony Binswanger extra point GOOD.
Anthony Binswanger kickoff for 69 yards returned by Michael Grandberry for 29 yards to the Memph 30.
DRIVE TOTALS: Marsh drive: 6 plays 66 yards, 03:29 Marsh TD
Memphis at 6:52 MRSH MEM
1st and 10 at MEM 30 Will Hudgens pass complete to Steven Black for 13 yards to the Memph 43 for a 1ST down.
1st and 10 at MEM 43 Will Hudgens pass complete to Earnest Williams for 9 yards to the Marsh 48.
2nd and 1 at MRSH 48 Joseph Doss rush for 2 yards to the Marsh 46 for a 1ST down.
1st and 10 at MRSH 46 Matt Malouf rush for 5 yards to the Marsh 41.
2nd and 5 at MRSH 41 Matt Malouf rush for 3 yards to the Marsh 38.
3rd and 2 at MRSH 38 Joseph Doss rush for 3 yards to the Marsh 35 for a 1ST down.
1st and 10 at MRSH 35 Joseph Doss rush for 3 yards to the Marsh 32.
2nd and 7 at MRSH 32 Will Hudgens pass incomplete to Carlos Singleton , broken up by Zearrick Matthews.
3rd and 7 at MRSH 32 Will Hudgens pass incomplete to Dave Thomas
4th and 7 at MRSH 32 Timeout MEMPHIS, clock 03:12.
4th and 7 at MRSH 32 Will Hudgens pass incomplete to Carlton Robinzine , broken up by Zearrick Matthews.
DRIVE TOTALS: Memph drive: 10 plays 38 yards, 03:46 Memph DOWNS
Marshall at 3:06 MRSH MEM
1st and 10 at MRSH 32 Bernard Morris pass incomplete to Darius Marshall
2nd and 10 at MRSH 32 Marshall penalty 10 yard holding on Brian Leggett accepted, no play.
2nd and 20 at MRSH 22 Bernard Morris rush for 4 yards to the Marsh 26.
3rd and 16 at MRSH 26 Bernard Morris pass incomplete to Cody Slate
4th and 16 at MRSH 26 Anthony Binswanger punt for 51 yards, returned by Earnest Williams for a loss of 3 yards to the Memph 20.
DRIVE TOTALS: Marsh drive: 3 plays -6 yards, 01:11 Marsh PUNT
Memphis at 1:55 MRSH MEM
1st and 10 at MEM 20 Will Hudgens pass complete to Joseph Doss for 3 yards to the Memph 23.
2nd and 7 at MEM 23 Will Hudgens pass complete to Steven Black for 10 yards to the Memph 33 for a 1ST down.
1st and 10 at MEM 33 Will Hudgens pass incomplete to Steven Black
2nd and 10 at MEM 33 Will Hudgens pass incomplete to Dave Thomas
3rd and 10 at MEM 33 Will Hudgens pass complete to Carlton Robinzine for 37 yards to the Marsh 30 for a 1ST down.
1st and 10 at MRSH 30 Joseph Doss rush for a loss of 1 yard to the Marsh 31.
2nd and 11 at MRSH 31 Timeout MEMPHIS, clock 00:39.
2nd and 11 at MRSH 31 Will Hudgens pass complete to Earnest Williams for 11 yards to the Marsh 20 for a 1ST down out-of-bounds.
1st and 10 at MRSH 20 Will Hudgens pass complete to Steven Black for 7 yards to the Marsh 13.
2nd and 3 at MRSH 13 Team pass incomplete.
3rd and 3 at MRSH 13 Will Hudgens pass incomplete.
4th and 3 at MRSH 13 Matt Reagan 30 yard field goal GOOD.
Matt Reagan kickoff for 50 yards returned by Darius Marshall for 33 yards to the Memph 47.
End of 2nd Quarter
3rd Quarter Play by Play Memphis at 15:00 MRSH MEM
Matt Reagan kickoff for 62 yards returned by Darius Marshall for 23 yards to the Marsh 31.
DRIVE TOTALS: Marsh drive: 1 play 0 yards, 00:04 Marsh END
HALF Marshall at 15:00 MRSH MEM
1st and 10 at MRSH 31 Bernard Morris rush for 10 yards to the Marsh 41 for a 1ST down.
1st and 10 at MRSH 41 Darius Marshall rush for 9 yards to the 50 yard line.
2nd and 1 at MEM 50 Darius Marshall rush for 15 yards to the Memph 35 for a 1ST down.
1st and 10 at MEM 35 Chubb Small rush for 12 yards to the Memph 23 for a 1ST down.
1st and 10 at MEM 23 Chubb Small rush for 7 yards to the Memph 16.
2nd and 3 at MEM 16 Darius Marshall rush for 5 yards to the Memph 11 for a 1ST down.
1st and 10 at MEM 11 Darius Marshall rush for 11 yards for a TOUCHDOWN. 13
Anthony Binswanger extra point GOOD. 14
Anthony Binswanger kickoff for 63 yards returned by Michael Grandberry for 17 yards to the Memph 14, Memphis penalty 10 yard illegal block on Tyler Griffin accepted.
DRIVE TOTALS: Marsh drive: 7 plays 69 yards, 02:58 Marsh TD
Memphis at 12:02 MRSH MEM
1st and 10 at MEM 14 Will Hudgens pass incomplete to Carlton Robinzine
14
2nd and 10 at MEM 14 Joseph Doss rush for 4 yards to the Memph 18.
3rd and 6 at MEM 18 Will Hudgens pass complete to Duke Calhoun for 23 yards to the Memph 41 for a 1ST down.
1st and 10 at MEM 41 Joseph Doss rush for 1 yard to the Memph 42.
2nd and 9 at MEM 42 Will Hudgens pass complete to Carlos Singleton for 8 yards to the 50 yard line.
3rd and 1 at MEM 50 T.J. Pitts rush for no gain to the 50 yard line.
4th and 1 at MEM 50 Joseph Doss rush for 2 yards to the Marsh 48 for a 1ST down.
1st and 10 at MRSH 48 Will Hudgens pass complete to Maurice Jones for 37 yards to the Marsh 11 for a 1ST down out-of-bounds.
1st and 10 at MRSH 11 Matt Malouf rush for 1 yard to the Marsh 10.
2nd and 9 at MRSH 10 Will Hudgens pass complete to Brett Russell for a loss of 7 yards, fumbled at the Marsh 19, recovered by Memph at the Marsh 19.
3rd and 18 at MRSH 19 Will Hudgens pass complete to Carlos Singleton for 19 yards for a TOUCHDOWN.
Two-point conversion attempt, Will Hudgens pass to Carlos Singleton GOOD.
Matt Reagan kickoff for 66 yards returned by Darius Marshall for 25 yards to the Marsh 29.
DRIVE TOTALS: Memph drive: 11 plays 86 yards, 05:10 Memph TD
Marshall at 6:52 MRSH MEM
1st and 10 at MRSH 29 Bernard Morris pass complete to E.J. Wynn for 7 yards to the Marsh 26, Marshall penalty 10 yard holding on Bryant Milligan accepted.
1st and 13 at MRSH 26 Bernard Morris pass complete to Brian Shope for 9 yards to the Marsh 35.
2nd and 4 at MRSH 35 Darius Marshall rush for 9 yards to the Marsh 44 for a 1ST down.
1st and 10 at MRSH 44 Chubb Small rush for 4 yards to the Marsh 48.
2nd and 6 at MRSH 48 Bernard Morris pass complete to E.J. Wynn for 2 yards to the 50 yard line.
3rd and 4 at MEM 50 Bernard Morris pass complete to Chubb Small for 10 yards to the Memph 40 for a 1ST down out-of-bounds.
1st and 10 at MEM 40 Darius Marshall rush for 7 yards to the Memph 33.
2nd and 3 at MEM 33 Darius Marshall rush for 9 yards to the Memph 24 for a 1ST down.
1st and 10 at MEM 24 Darius Marshall rush for 1 yard, fumbled at the Memph 23, recovered by Quinton McCrary at the Memph 23.
DRIVE TOTALS: Marsh drive: 9 plays 48 yards, 03:51 Marsh FUMB
Memphis at 3:01 MRSH MEM
1st and 10 at MEM 23 Earnest Williams rush for 3 yards to the Memph 26.
2nd and 7 at MEM 26 Will Hudgens pass complete to Earnest Williams for 12 yards to the Memph 38 for a 1ST down out-of-bounds.
1st and 10 at MEM 38 T.J. Pitts rush for 6 yards to the Memph 44.
2nd and 4 at MEM 44 T.J. Pitts rush for 13 yards to the Marsh 43 for a 1ST down.
1st and 10 at MRSH 43 Will Hudgens pass complete to Carlos Singleton for 3 yards to the Marsh 40.
2nd and 7 at MRSH 40 Will Hudgens pass incomplete to Duke Calhoun
3rd and 7 at MRSH 40 Will Hudgens pass complete to Carlos Singleton for 35 yards to the Marsh 5 for a 1ST down.
1st and Goal at MRSH 5 T.J. Pitts rush for 1 yard to the Marsh 4 out-of-bounds.
End of 3rd Quarter
4th Quarter Play by Play Memphis at 15:00 MRSH MEM
2nd and Goal at MRSH 4 Will Hudgens pass complete to Greg Hinds for 4 yards for a TOUCHDOWN.
Matt Reagan extra point GOOD.
Matt Reagan kickoff for 69 yards returned by Emmanuel Spann for 23 yards to the Marsh 24.
DRIVE TOTALS: Memph drive: 9 plays 77 yards, 02:44 Memph TD
Marshall at 14:55 MRSH MEM
1st and 10 at MRSH 24 Bernard Morris rush for 12 yards to the Marsh 36 for a 1ST down.
1st and 10 at MRSH 36 Chubb Small rush for 22 yards to the Memph 27 for a 1ST down, Memphis penalty 15 yard face mask on Will Hudgens accepted.
1st and 10 at MEM 27 Bernard Morris pass complete to Darius Passmore for 15 yards to the Memph 12 for a 1ST down.
1st and 10 at MEM 12 Chubb Small rush for a loss of 2 yards to the Memph 14.
2nd and 12 at MEM 14 Bernard Morris pass complete to Cody Slate for 6 yards to the Memph 8 out-of-bounds.
3rd and 6 at MEM 8 Bernard Morris pass incomplete to Courtney Edmonson
4th and 6 at MEM 8 Emmanuel Spann rush for a loss of 8 yards to the Memph 16.
DRIVE TOTALS: Marsh drive: 7 plays 60 yards, 02:38 Marsh DOWNS
Memphis at 12:17 MRSH MEM
1st and 10 at MEM 16 T.J. Pitts rush for 7 yards to the Memph 23.
2nd and 3 at MEM 23 T.J. Pitts rush for 2 yards to the Memph 25.
3rd and 1 at MEM 25 Matt Malouf rush for 2 yards to the Memph 27 for a 1ST down.
1st and 10 at MEM 27 Will Hudgens pass complete to Carlos Singleton for 4 yards to the Memph 31.
2nd and 6 at MEM 31 Will Hudgens pass complete to Duke Calhoun for 2 yards to the Memph 33.
3rd and 4 at MEM 33 Will Hudgens pass complete to Maurice Jones for 3 yards to the Memph 36.
4th and 1 at MEM 36 Timeout MEMPHIS, clock 08:04.
4th and 1 at MEM 36 Brent Sutherland punt for 33 yards punt out-of-bounds at the Marsh 31.
DRIVE TOTALS: Memph drive: 6 plays 20 yards, 04:13 Memph PUNT
Marshall at 8:04 MRSH MEM
1st and 10 at MRSH 31 Bernard Morris sacked by Josh Weaver at the Marsh 24 for a loss of 8 yards.
2nd and 17 at MRSH 24 Bernard Morris rush for 13 yards to the Marsh 37.
3rd and 4 at MRSH 37 Bernard Morris pass complete to Darius Passmore for 14 yards to the Memph 49 for a 1ST down.
1st and 10 at MEM 49 Bernard Morris pass incomplete to Chubb Small
2nd and 10 at MEM 49 Bernard Morris pass complete to Emmanuel Spann for 20 yards to the Memph 29 for a 1ST down.
1st and 10 at MEM 29 Bernard Morris pass complete to Cody Slate for 11 yards to the Memph 18 for a 1ST down.
1st and 10 at MEM 18 Bernard Morris pass complete to Darius Passmore for 5 yards to the Memph 13 out-of-bounds.
2nd and 5 at MEM 13 Bernard Morris pass complete to Shawn Lauzon for 9 yards to the Memph 4 for a 1ST down.
1st and Goal at MEM 4 Bernard Morris pass incomplete to Bryant Milligan
2nd and Goal at MEM 4 Bernard Morris pass incomplete to Emmanuel Spann
3rd and Goal at MEM 4 Bernard Morris pass complete to Darius Passmore for 4 yards for a TOUCHDOWN.
Anthony Binswanger extra point GOOD.
Anthony Binswanger kickoff for 60 yards returned by Michael Grandberry for 14 yards to the Memph 24, fumbled at the Memph 24, recovered by Memph.
DRIVE TOTALS: Marsh drive: 11 plays 69 yards, 02:35 Marsh TD
Memphis at 5:22 MRSH MEM
1st and 10 at MEM 24 Will Hudgens pass complete to Duke Calhoun for 6 yards to the Memph 30.
2nd and 4 at MEM 30 Marshall penalty 15 yard pass interference on Zearrick Matthews accepted, no play.
1st and 10 at MEM 45 T.J. Pitts rush for 4 yards to the Memph 49.
2nd and 6 at MEM 49 Will Hudgens pass complete to Maurice Jones for 7 yards to the Marsh 44 for a 1ST down out-of-bounds.
1st and 10 at MRSH 44 T.J. Pitts rush for 2 yards to the Marsh 42.
2nd and 8 at MRSH 42 Will Hudgens pass incomplete to Carlton Robinzine
3rd and 8 at MRSH 42 Will Hudgens pass incomplete to Carlton Robinzine
4th and 8 at MRSH 42 Timeout MEMPHIS, clock 02:34.
4th and 8 at MRSH 42 Memphis penalty delay of game declined.
4th and 8 at MRSH 42 Brent Sutherland punt for 42 yards for a touchback.
DRIVE TOTALS: Memph drive: 6 plays 34 yards, 02:56 Memph PUNT
Marshall at 2:34 MRSH MEM
1st and 10 at MRSH 20 Bernard Morris pass complete to Darius Passmore for 11 yards to the Marsh 31 for a 1ST down.
1st and 10 at MRSH 31 Bernard Morris pass complete to Cody Slate for 9 yards to the Marsh 40 out-of-bounds.
2nd and 1 at MRSH 40 Bernard Morris pass complete to Darius Passmore for 12 yards to the Memph 48 for a 1ST down.
1st and 10 at MEM 48 Bernard Morris pass complete to Cody Slate for 6 yards to the Memph 42 out-of-bounds.
2nd and 4 at MEM 42 Team rush for a loss of 19 yards to the Marsh 39.
3rd and 23 at MRSH 39 Bernard Morris rush for 20 yards to the Memph 41.
4th and 3 at MEM 41 Timeout MARSHALL, clock 01:04.
4th and 3 at MEM 41 Bernard Morris pass incomplete.
DRIVE TOTALS: Marsh drive: 7 plays 39 yards, 01:30 Marsh DOWNS
Memphis at 1:04 MRSH MEM
1st and 10 at MEM 41 Team rush for a loss of 1 yard to the Memph 40.
2nd and 11 at MEM 40 Team rush for a loss of 1 yard to the Memph 39.
End of 4th Quarter
答案 0 :(得分:2)
这绝对不是最好的方法,但是如果你确定数据会以这种格式出现,那么这个(草率的)代码应该可以工作:
>>> commentary = #your commentary text
>>>
>>> # Returns the string up to the timestamp.
... # quarter is an int, 1-4, time is int, 0-15.
... def getPlayByPlay(commentary, quarter, time):
... reString = r'\w\w Quarter' #1st Quarter, 2nd Quarter, etc.
... quarterDex = re.search(str(quarter) + reString, commentary).start()
... endQuarterDex = 0
... if quarter == 4:
... endQuarterDex = len(commentary)
... else:
... endQuarterDex = re.search(str(quarter + 1) + reString, commentary).start()
... for match in re.finditer(r'at \d\d:\d\d', commentary[quarterDex:endQuarterDex]):
... matchTime = int(match.group(0)[3:5])
... print matchTime
... if matchTime < time:
... return commentary[:quarterDex + match.start()]
... return commentary[:endQuarterDex]
>>> getPlayByPlay(commentary, 1, 3)
'\n1st Quarter Play by Play ... End of 1st Quarter \n'
>>> getPlayByPlay(commentary, 1, 13)
'\n1st Quarter Play by Play ... 02:12 Memph FG\n Marshall '
>>> getPlayByPlay(commentary, 4, 0)
gives the whole string
如您所见,没有错误检查,这完全取决于我在这个具体示例中找到的模式。如果quarter不是1到4之间的int
或者时间不是而且int
介于0和15之间,则会导致崩溃或返回错误。请确保您注意到time
是int(否)小数) - 如有必要,您可以进行更复杂的分析。希望它有用!