- ?
import re
def f_OK(ch):
print 'BINGO ! : %s , %s' % re.match('OK\s+(\w+)\s+(\w+)',ch).groups()
def f_FAIL(ch):
print 'only one : ' + ch.split()[-1]
several_func = (f_OK, f_FAIL)
several_REs = ('OK\s+\w+\s+\w+',
'FAILED\s+\w+')
globpat = re.compile(')|('.join(several_REs).join(('^(',')$')))
with open('big_input.txt') as handle:
for i,line in enumerate(handle):
print 'line '+str(i)+' - ',
mat = globpat.search(line)
if mat:
several_func[mat.lastindex-1](mat.group())
else:
print '## no match ## '+repr(line)
, :
OK tiramisu sunny
FAILED overclocking
FAILED nuclear
E = mcXc
OK the end
line 0 - BINGO ! : tiramisu , sunny
line 1 - only one : overclocking
line 2 - only one : nuclear
line 3 - ## no match ## 'E = mcXc\n'
line 4 - BINGO ! : the , end
RE , , ..