Parsing multiple lines from text file using regex
Rhodri James
rhodri at wildebst.demon.co.uk
Sun Oct 27 18:19:41 EDT 2013
On Sun, 27 Oct 2013 21:09:46 -0000, Marc <marc at marcd.org> wrote:
> 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 really, really wouldn't do this with a single regexp. You'll get a much
easier to understand program if you implement a small state machine
instead. In rough pseudocode:
collecting_banner = False
for line in configuration_file:
if not collecting_banner:
if found banner start:
get delimiter
collecting_banner = True
banner_lines = []
elif found other stuff:
do other stuff
elif found delimiter:
collecting_banner = False
else:
banner_lines.append(line)
--
Rhodri James *-* Wildebeest Herder to the Masses
More information about the Python-list
mailing list