hangon has asked for the wisdom of the Perl Monks concerning the following question:
I need to strip comments from a configuration file. The comments start with # and go to the end of the line, unless the initial # is escaped. This is what I have:
while (my $line = <FH>){ $line =~ s/[^\\]#.*//; $line =~ s/^#.*//; $line =~ s/\\#/#/g; print "$line"; }
The second regex is to handle cases where the line begins with #. The third just removes the escape character. This works, but is there a better way or can it be simplified with with fewer regexes? Thanks.
sample data:
\#Not a comment #Comment \#Not a comment #But this is a comment arbitrary text #Comment arbitrary text \#Not a comment arbitrary text \#Not a comment #But this is a comment arbitrary text #Comment \#This is part of the comment
desired output:
#Not a comment #Not a comment arbitrary text arbitrary text #Not a comment arbitrary text #Not a comment arbitrary text
UPDATE: Got what I needed, and a little education too. Thanks for the help everyone.
Back to
Seekers of Perl Wisdom