in reply to Re: Regex in a printfile?
in thread Regex in a printfile?

That removes all empty lines, not just those that were comments. Fix:

while (<INFILE>) { chomp; next if /^\s*#/; s/\s*#.*$//; print OUTFILE "$_\n"; }

Replies are listed 'Best First'.
Re^3: Regex in a printfile?
by johngg (Canon) on Nov 15, 2006 at 20:53 UTC
    The problem with s/\s*#.*$//; is that you might start breaking scripts unexpectedly. Given this (contrived but not unlikely) code snippet

    my @fred = (1, 2, 3, 4, 5); my $lastSub = $#fred; print qq{$lastSub\n};

    running your substitution will result in

    my @fred = (1, 2, 3, 4, 5); my $lastSub = $ print qq{$lastSub\n};

    Perhaps something like

    s{(?<!\$)#.*$}{};

    would be better but there may be other syntax where the hash is not the start of a comment that I haven't thought of.

    Cheers,

    JohnGG

    Update: Found another non-comment occurrence of the # character in the colour values in Tk programs, e.g.

    -background => #101010,

    so perhaps now

    s{(?<!\$)#(?![0-9a-fA-F]{6}).*$}{};