Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: regex basics

by choroba (Cardinal)
on Feb 02, 2015 at 15:28 UTC ( [id://1115294]=note: print w/replies, xml ) Need Help??


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

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.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^6: regex basics
by AnomalousMonk (Archbishop) on Feb 02, 2015 at 19:13 UTC
    ! $_ =~ /;\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:  <%-(-(-(-<

Re^6: regex basics
by Anonymous Monk on Feb 04, 2015 at 07:18 UTC

    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: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115294]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-25 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found