However, your code will stop working if you consider
my $string = "--This is a comment\nNot a comment\n" . "--This Comment continues here\nNOT a comment";
Comments do not necessarily occur at the beginning of the (rest of the) string. (At least that's how I understood the question.) I propose to simply do
my $string = "..."; $string =~ s/(^|\n)--[^\n]+//g; print "No comments: $string\n";
If you're perfectionistic, you will notice that this will leave a leading newline if the string starts with a comment. You can fix that by using
my $string = "..."; $string =~ s/\n--[^\n]+//g; $string =~ s/^--[^\n]+\n//; print "No comments: $string\n";
In reply to Re: Re: Removing nested comments with regexes
by crenz
in thread Removing nested comments with regexes
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |