hifirock has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i want to remove the certain log lines if its starts with -- or remove the words after the --, but if it is present within the double quotes(" test -- done")then no need of substitution.
for Example :
line1: -- test the line,8,description; (Need to remove the line)
line2:test the line,8,description="testing -- done"; (No need)
line3: test the line,8,description="testing -- done"; -- commented line (need to remove the words after the -- symbol)
if i remove with  s/(?<!\").*--.*(?<!\")//g its not working,ca n anyone help me?

Replies are listed 'Best First'.
Re: regex match only outside the double quotes
by moritz (Cardinal) on Aug 21, 2009 at 07:57 UTC
    First extract the quoted groups (with something like Text::CSV or Text::Balanced or even a regex), then look in the remainder for a --.

    That's going to be much easier than finding a "one size fits all" regex.

    Perl 6 projects - links to (nearly) everything that is Perl 6.
Re: regex match only outside the double quotes
by JavaFan (Canon) on Aug 21, 2009 at 08:51 UTC
    You have three cases:
    • Line start with --, you want to remove the entire line aka replace with nothing.
    • A substring starting and ending with " should be left alone, aka replace with itself.
    • -- elsewhere should have the part after the -- removed, aka, replace the entire thing with --.
    This is trivial in a regexp:
    s/("[^"]*")|^--.*\n|(--).*/$1||$2||""/egm;
        1. The OP never said anything about escaped quotes, which I why I didn't include them
        2. I can extend the regexp to include escapes in my sleep, which both hands tied behind my back. And I'd include loop unrolling.
Re: regex match only outside the double quotes
by sanku (Beadle) on Aug 21, 2009 at 09:38 UTC
    hi, Try out this one it will work fine. test.txt -- test the line,8,description; (Need to remove the line) test the line,8,description="testing -- done"; (No need) test the line,8,description="testing -- done"; -- commented line (need to remove the words after the -- symbol)
    open(FILE,"test.txt") or die $!; while($line=<FILE>){ $line=~s/-- .*//g; print "$line\n"; }