Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Removing multiple trailing comment lines from a string

by eyepopslikeamosquito (Archbishop)
on Dec 23, 2016 at 21:14 UTC ( [id://1178443]=note: print w/replies, xml ) Need Help??


in reply to Re: Removing multiple trailing comment lines from a string
in thread Removing multiple trailing comment lines from a string

Is this what you want?
Yep. Thanks. I was hoping it could be done with a single regex, but your solution looks good.

  • Comment on Re^2: Removing multiple trailing comment lines from a string

Replies are listed 'Best First'.
Re^3: Removing multiple trailing comment lines from a string
by tybalt89 (Monsignor) on Dec 23, 2016 at 21:59 UTC

    That looks wrong to me because it removes more than three lines.

    Does this look correct?

    #!/usr/bin/perl use strict; use warnings; # Given an ini file, return a string containing section contents. # (Note that ini file comment lines start with a ;) sub get_section { my $fcontents = shift; # in: ini file contents string my $section = shift; # in: section name to get # Note that the regex below will find multiple sections; # it's terminated by the start of a new section or end of file. my $s = join( "", $fcontents =~ /^[ \t]*\[$section\](.*?)(?:\Z|^[ \ +t]*\[)/msg ); $s =~ s/^[ \t]+//mg; # remove leading whitespace from each line $s =~ s/[ \t]+$//mg; # remove trailing whitespace from each line $s =~ s/^\s+//; # remove leading whitespace $s =~ s/\s+$//; # remove trailing whitespace # Remove up to three trailing comment lines $s =~ s/ ( \n (;.*)? ){1,3} \z //x; # hopefully less of an eyesore return $s; } my $inifile_contents = <<'BUK_LIKES_SUNDIALS'; [MySection] ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 ; comment 1 inside the section ; comment 2 inside the section fld3 =89 ; trailer ; trailer 2 ; trailer 3 ; trailer 4 ; trailer 5 ; This is the heading for AnotherSection [AnotherSection] ; another comment asfld=69 BUK_LIKES_SUNDIALS my $section1 = get_section( $inifile_contents, 'MySection' ); print "This is the contents of MySection -------\n$section1\n"; my $section2 = get_section( $inifile_contents, 'AnotherSection' ); print "This is the contents of AnotherSection -------\n$section2\n";

    which prints

    This is the contents of MySection ------- ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 ; comment 1 inside the section ; comment 2 inside the section fld3 =89 ; trailer ; trailer 2 ; trailer 3 ; trailer 4 This is the contents of AnotherSection ------- ; another comment asfld=69

      Thanks, that's the "single regex" solution I couldn't see. Sorry I was vague about the requirements; whether the last three or the last n I'm not too worried, just wanted to explore alternative ways to do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1178443]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found