I am trying to use regex in Python to find and print all matching lines from a multiline search.
The text that I am searching through may have the below example structure:
AAA
ABC1
ABC2
ABC3
AAA
ABC1
ABC2
ABC3
ABC4
ABC
AAA
ABC1
AAA
From which I want to retrieve the ABC*s that occur at least once and are preceeded by an AAA.
The problem is, that despite the group catching what I want:
match = <_sre.SRE_Match object; span=(19, 38), match='AAA
ABC2
ABC3
ABC4
'>
... I can access only the last match of the group:
match groups = ('AAA
', 'ABC4
')
Below is the example code that I use for this problem.
#! python
import sys
import re
import os
string = "AAA
ABC1
ABC2
ABC3
AAA
ABC1
ABC2
ABC3
ABC4
ABC
AAA
ABC1
AAA
"
print(string)
p_MATCHES = []
p_MATCHES.append( (re.compile('(AAA
)(ABC[0-9]
){1,}')) ) #
matches = re.finditer(p_MATCHES[0],string)
for match in matches:
strout = ''
gr_iter=0
print("match = "+str(match))
print("match groups = "+str(match.groups()))
for group in match.groups():
gr_iter+=1
sys.stdout.write("TEST GROUP:"+str(gr_iter)+""+group) # test output
if group is not None:
if group != '':
strout+= '"'+group.replace("
","",1)+'"'+'
'
sys.stdout.write("
COMPLETE RESULT:
"+strout+"====
")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…