I looked again at this thread and at kcott's line by line solution at Re: Removing multiple trailing comment lines from a string
I would also be thinking of parsing line by line instead of as string.
I've shown another method for that.

Some general comments re: .ini files:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoA; while (my $line=<DATA>) { my $next_line = process_section ($1) if $line =~ /\s*\[\s*(\S+)\s*\ +]/; if (defined $next_line) {$line = $next_line; redo} } print Dumper \%HoA; sub process_section { my $section = shift; my $line; while (defined ($line=<DATA>) and $line !~ /\s*\[\s*(\S+)\s*\]/) { chomp $line; next if $line =~ /^\s*$/; # skip blank lines push @{$HoA{$section}}, $line; } # delete the "trailing comments" in this [section] heading my $comment; while ($comment = pop @{$HoA{$section}} and $comment =~ /^\s*;/ ){} +; push @{$HoA{$section}}, $comment; return $line; } =prints $VAR1 = { 'AnotherSection' => [ '; another comment', 'asfld=69' ], 'MySection' => [ '; This is a comment line for MySection', 'fld1 = \'value of field 1\' ', 'fld2 = 42' ], 'WhitespaceSection' => [ ' ; Comment starting with a tab' +, ' ; Comment starting with a tab + and a space', ' ; Comment starting with a space', '; Comment ending with a tab ', '; Comment ending with a tab and a +space ', '; Comment ending with a space ', ' ; tab+space+comment+space+tab + ', ' ; space+tab+comment+tab+space + ', 'qwe=rty', 'asd=fgh' ] }; =cut __DATA__ ; this is root section a = 2 ; some comment in root b = 3 ; some trailing comment in root [MySection] ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 ; This is the heading for AnotherSection [AnotherSection] ; another comment asfld=69 ; Heading for WhitespaceSection [WhitespaceSection] ; Comment starting with a tab ; Comment starting with a tab and a space ; Comment starting with a space ; Comment ending with a tab ; Comment ending with a tab and a space ; Comment ending with a space ; tab+space+comment+space+tab ; space+tab+comment+tab+space qwe=rty asd=fgh ; trailing 1 ; tab + trailing 2 ; space + trailing 3 ; trailing 4
PS: my previous code explicitly allowed more than 3 trailing comments because I thought that was a requirement and was one of the "problems". I am also not sure why some .ini file comments should be ignored and others not? That is a strange thing to me.

In reply to Re: Removing multiple trailing comment lines from a string by Marshall
in thread Removing multiple trailing comment lines from a string by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.