Skip to main content


I have an input string that has "comments" in it that are in the form of: everything after a ; is considered a comment. I want to remove all comments from the string and get the raw text out. This is a pretty simple regexp replace except for the fact that ; characters are valid non-comments inside of double quoted strings.

How can I improve the remove_comments() function to handle quoted strings with semi-colons in them correctly?

```perl use v5.36;

my $str = 'Raw data ; This is a full-line comment More data ; comment after it Some data "and a quoted string; this is NOT a comment"';

my $clean = remove_comments($str);

print "Before:\n$str\n\nAfter:\n$clean\n";

sub remove_comments { my $in = shift();

$in =~ s/;.*//g; # Remove everything after the ; to EOL return $in; 

} ```

submitted by /u/scottchiefbaker
[link] [comments]