我喜欢正则表达式。我经常发现自己使用多个regex语句来缩小从大量文本块获取子字符串时所需的值。
到目前为止,我的做法如下:
resultOfRegex1 = re.findall(firstRegex, myString)
resultOfRegex1[0]
resultOfRegex2 = re.findall(secondRegex, resultOfRegex1[0])
resultOfRegex2[0]
,并打印该值但我觉得这比必须要详细和昂贵得多。是否有更容易/更快的方法来匹配一个正则表达式,然后根据第一个正则表达式的结果匹配另一个正则表达式?
发布于 2017-03-18 18:20:11
组的全部目的是允许从整体匹配中提取子组。
例如,两个搜索完成了以下方式:
>>> import re
>>> s = 'The winning team scored 15 points and used only 2 timeouts'
>>> score_clause = re.search(r'scored \d+ point', s).group(0)
>>> re.search(r'\d+', score_clause).group(0)
'15'
使用一个子组进行一次搜索:
>>> re.search(r'scored (\d+) point', s).group(1)
'15'
另一个想法是:如果您想根据第一次匹配来决定是否继续进行findall样式的搜索,一个合理的选择是使用re.finditer
并根据需要提取值:
>>> game_results = '''\
10 point victory: 1 in first period, 6 in second period, 3 in third period.
5 point victory: 0 in first period, 5 in second period, 0 in third period.
12 point victory: 5 in first period, 3 in second period, 4 in third period.
7 point victory: 3 in first period, 0 in second period, 4 in third period.
'''.splitlines()
>>> # Show period-by-period scores for games won by 8 or more points
>>> for game_result in game_results:
it = re.finditer(r'\d+', game_result)
if int(next(it).group(0)) >= 8:
print 'Big win:', [int(mo.group(0)) for mo in it]
Big win: [1, 6, 3]
Big win: [5, 3, 4]
https://stackoverflow.com/questions/42880387
复制