in reply to Re^3: regex basics
in thread regex basics

CountZero, did you say !$_ /;\s+/ is invalid syntax?

And here I was all ready to use this on other config files. If fact, I already have:

#!/usr/bin/env perl use strict; use warnings; my $conforig = 'httpd.conf-orig'; my $confnocm = 'httpd.conf-nocomments'; open my $IN, '<', $conforig or die "Could not open $conforig for reading: $!"; my $text; while (<$IN>) { if (!/\#\s+/) { $text .= $_; } } close $IN or die "Error closing $conforig: $!"; # remove blank lines $text =~ s{\n{2,}}{\n}gm; open my $OUT, '>', $confnocm or die "Could not open $confnocm for writing: $!"; print $OUT $text; close $OUT or die "Error closing $confnocm: $!";
It seems to work... but please show me the correct negation syntax before the code police smash in my door, drag me off to a detention camp and force me to write JavaScript.

Replies are listed 'Best First'.
Re^5: regex basics
by choroba (Cardinal) on Feb 02, 2015 at 15:28 UTC
    These are not the same:
    !$_ /;\s+/ !/\#\s+/

    In fact, when you want to mention the thing to be matched, you must use the binding operator:

    !( $_ =~ /;\s+/); # or $_ !~ /;\s+/;

    Update: Precedence fixed. Thanks AnomalousMonk.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      ! $_ =~ /;\s+/;

      Because of the high precedence of the  ! (logical-not) operator versus  =~ (see perlop), the regex actually matches against either  '' (the empty string) or '1'.

      c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "! $_ =~ /;\s+/; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; ((!$_) =~ /;\s+/); -e syntax OK
      These are equivalent:
      ! ($_ =~ /;\s+/); # or $_ !~ /;\s+/;


      Give a man a fish:  <%-(-(-(-<

      Understood.

      =~ means match and !~ means no match.

      This should work:

      #!/usr/bin/env perl use strict; use warnings; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; open my $IN, '<', $iniprod or die "Could not open $iniprod for reading: $!"; my $text; while (<$IN>) { if ($_ !~ /;\s+/) { $text .= $_; } } close $IN or die "Error closing $iniprod: $!"; # remove blank lines $text =~ s{\n{2,}}{\n}gm; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writing: $!"; print $OUT $text; close $OUT or die "Error closing $ininocm: $!";