I am new to python and regular expressions. I am currently trying to make a program that reads the contents of the file below and get specific parameters and max_speeds within the sections. Under each SECTION:#, the parameters are all indented (TAB) until the next SECTION:#
[SECTION:3]
paramter = 3
state = AZ
car = toyota
max_speed = 90.000
any_pets = yes
[SECTION:13]
paramter = 10
state = NY
car = honda
max_speed = 120.000
any_pets = yes
[SECTION:85]
paramter = 31
state = TX
car = kia
max_speed = 30.000
any_pets = no
This is my code:
import re
file = open('file.txt').readlines()
file_str = str(file)
for lines in file_str:
myreg = re.compile(r'(?<=SECTION:13).+(max_speed\s\=\s\w+)')
myreg1 = myreg.search(lines)
print myreg1.group(1)
The problem is that the results are always wrong...it's as if the regular expression always matches the results of the last section.
Please let me know what am i doing wrong and what would be the best way of doing it. Thank you!