I want to perform the selection of a group of lines in a text file to get all jobs related to an ipref The test file is like this : job numbers : (1,2,3), ip ref : (10,12,10)
text file : 1 ... (several lines of text) xxx 10 2 ... (several lines of text) xxx 12 3 ... (several lines of text) xxx 10
i want to select job numbers for IPref=10.
Code :
#!/usr/bin/python
import re
import sys
fic=open('test2.xml','r')
texte=fic.read()
fic.close()
#pattern='\n?\d(?!(?:\n?xxx \d{2}\n)*)xxx 10'
pattern='\n?\d.*?xxx 10'
result= re.findall(pattern,texte, re.DOTALL)
i=1
for match in result:
print("\nmatch:",i)
i=i+1
print(match)
Result :
match: 1
1
a
b
xxx 10
match: 2
1
a
b
xxx 12
1
a
b
xxx 10
i have tried to replace .* by a a negative lookahead assertion to only select if no expr like "\n?xxx \d{2}\n" is before "xxx 10" :
pattern='\n?\d(?!(?:\n?xxx \d{2}\n)*)xxx 10'
but it is not working ...