I have a text file in the form like below
sourceFiles {
hello.cpp,
hi.cpp,
main.cpp,
}
headerFiles {
hello.h,
hi.h,
}
path {
source [
what,
how,
which,
]
header [
include,
inc,
head,
]
}
I need to populate several lists from this text file. So far I have done this
sourcePath = None
headerPath = None
regex = r'sourceFiles\s*\{(.*?)\}'
match = re.findall(regex, data, re.DOTALL | re.IGNORECASE)
sources = [re.findall(r'\w+.\w+', each) for each in match][0]
regex = r'headerFiles\s*\{(.*?)\}'
match = re.findall(regex, data, re.DOTALL | re.IGNORECASE)
headers = [re.findall(r'\w+.\w+', each) for each in match][0]
regex = r'path\s*\{(.*?)\}'
match = re.findall(regex, data, re.DOTALL | re.IGNORECASE)
paths = [re.findall(r'\[.*?\]', each, re.DOTALL) for each in match][0]
if paths:
temp = re.findall(r'\[(.+?)\]', paths[0], re.DOTALL)
sourcePath = [re.findall(r'(\w+)\,', each) for each in temp][0]
temp = re.findall(r'\[(.+?)\]', paths[1], re.DOTALL)
headerPath = [re.findall(r'(\w+)\,', each) for each in temp][0]
print sources
print headers
print sourcePath
print headerPath
But this has several problems. One of the problem is that if I don't provide a , after all the entries, it does not give me any exception. I want that if I don't provide , after every entry, it would generate an exception. Moreover I want much stronger syntax checking of this file. Please help me rewriting it.