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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stripping Comments
by varian (Chaplain) on Jun 09, 2007 at 09:18 UTC | |
|
Re: Stripping Comments (one)
by tye (Sage) on Jun 10, 2007 at 02:28 UTC | |
|
Re: Stripping Comments
by moritz (Cardinal) on Jun 09, 2007 at 09:10 UTC | |
by betterworld (Curate) on Jun 10, 2007 at 00:54 UTC |