Hi,
I am having an issue with something that would seem to have an easy solution, but which escapes me. I have configuration files that I would like to parse. The data I am having issue with is a multi-line attribute that has the following structure:
banner <option> <banner text delimiter>
Banner text
Banner text
Banner text
...
<banner text delimiter>
The regex 'banner\s+(\w+)\s+(.+)' captures the command nicely and banner.group(2) captures the delimiter nicely.
My issue is that I need to capture the lines between the delimiters (both delimiters are the same).
I have tried various permutations of
Delimiter=banner.group(2)
re.findall(Delimiter'(.*?)'Delimiter, line, re.DOTALL|re.MULTILINE)
with no luck
Examples I have found online all assume that the starting and ending delimiters are different and are defined directly in re.findall(). I would like to use the original regex extracting the banner.group(2), since it is already done, if possible.
Any help in pointing me in the right direction would be most appreciated.
Thank you,
Marc
Hi,
I am having an issue with something that would seem to have an easy solution, but which escapes me. I have configuration files that I would like to parse. The data I am having issue with is a multi-line attribute that has the following structure:
banner <option> <banner text delimiter>
Banner text
Banner text
Banner text
...
<banner text delimiter>
> This is an alternative solution someone else posted on this list for a similar problem I had:
> #!/usr/bin/python3
> from itertools import groupby
> def get_lines_from_file(file_name):
> with open(file_name) as reader:
> for line in reader.readlines():
> yield(line.strip())
> counter = 0
> def key_func(x):
> if x.strip().startswith("banner") and x.strip().endswith("<banner text delimiter>"):
> global counter
> counter += 1
> return counter
> for key, group in groupby(get_lines_from_file("my_data"), key_func):
> print(list(group)[1:-1])
Thanks Jason,
banner = re.compile(r'banner\s+(\w+)\s+(.+)(.*?)\2', re.DOTALL).findall(lines)
worked nicely to get what I needed:
outfile.write("Banner type: %s Banner Delimiter: %s\n" % (banner[0][0], banner[0][1]))
outfile.write("Banner Text:\n")
outfile.write(banner[0][2])
Probably not the prettiest, most concise code, but it gets the job done.
Thanks again,
Marc