in reply to Re^2: Parsing by indentation
in thread Parsing by indentation
Same code with expanded regex with comments
#!/usr/bin/perl # https://perlmonks.org/?node_id=1224600 use strict; use warnings; use Data::Dump 'dd'; my $data = <<'END'; interface XYZ given param1 -> child of "interface XYZ" given param2 -> child of "interface XYZ" given param2.1 -> child of "given param2" given param2.1.1 -> child of "given param2.1" given param2.1.2 -> child of "given param2.1" given param2.2 -> child of "given param2" given param3 -> child of "interface XYZ" given param4 -> child of "interface XYZ" interface SECOND given param5 -> child of "interface SECOND" END my $struct = buildstruct($data); dd $struct; sub buildstruct { my $block = shift; my @answers; while( $block =~ /^ # make sure to start at beginning of a line (wit +h m) (\ *) # match leading spaces of header line (.*) # match rest of line, save as head \n # and match the newline ( (?: # match all following lines with \1 # same whitespace as head \ + # plus at least one more space ( i.e. indented ) .*\n # contents to be looked at later )* # as many as possible ) # save as rest /gmx ) # global, multiline, and extra whitespace { my ($head, $rest) = ($2, $3); $head =~ s/ ->.*//; push @answers, $rest ? { $head => buildstruct($rest) } : $head; } \@answers; }
I hope this helps :)
The regex matches each line, gets the indentation space string, then also matches all
following lines that are indented that much plus at least one more space.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Parsing by indentation
by llarochelle (Beadle) on Oct 25, 2018 at 02:14 UTC | |
Re^4: Parsing by indentation
by llarochelle (Beadle) on Oct 25, 2018 at 14:13 UTC | |
by hippo (Archbishop) on Oct 25, 2018 at 15:16 UTC | |
by llarochelle (Beadle) on Oct 26, 2018 at 13:41 UTC | |
by haukex (Archbishop) on Oct 26, 2018 at 14:00 UTC | |
by AnomalousMonk (Archbishop) on Oct 25, 2018 at 17:02 UTC |