Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regex AND NOT with zero-width negative lookahead assertion

by hippo (Bishop)
on Mar 25, 2020 at 12:13 UTC ( [id://11114634]=note: print w/replies, xml ) Need Help??


in reply to Regex AND NOT with zero-width negative lookahead assertion

I think this might do what you want.

use strict; use warnings; use Test::More tests => 1; my $want = <<'EOT'; if(/ok .*$/) { if(m/a good match/( { $string =~ s/[a-f]//g; # but print this line ( $string ) = ( $string =~ /(show this)/)); /but show\/this, too/ EOT my $have = ''; while (<DATA>) { $have .= $_ if m~^(?!#)[^/]*/[^/]+(\\/|/(?!\w+/?))~; } is $have, $want; __DATA__ #!/usr/bin/perl foo bar foo/bar /src/bin/oops/otehnoes if(/ok .*$/) { print "not OK\n"; } # skip a comment if(m/a good match/( { print "not ok\n"; } # do not /print/ this line either $string =~ s/[a-f]//g; # but print this line ( $string ) = ( $string =~ /(show this)/)); /but show\/this, too/ my $butdontprintthis = "/var/cache/dictionaries-common";

Replies are listed 'Best First'.
Re^2: Regex AND NOT with zero-width negative lookahead assertion
by mldvx4 (Friar) on Mar 25, 2020 at 13:34 UTC

    Thanks. (And thanks to those below, too.)

    That seems to achieve the desired results, if I escape the pound sign (#) there. Otherwise that gets interpreted as a comment.

      How odd. It works fine exactly as posted for me in Perl 5.20.3. Which Perl version are you using which requires the escape?

      Update: also tested successfully on v5.10.1, v5.16.3, v5.26.1 and v5.30.0.

        I believe this works everywhere; certainly does on 5.8. What *doesn’t* work a lot of the time is syntax highlighting and autoformatting. I end up escaping them sometimes just so I don’t have to deal with that; in certain codebases/c?perl-modes at least.

        The  m, ... ,x match in the OPed code uses the  /x whitespace/comments modifier; your match does not. (Update: I presume mldvx4 is still using the  /x modifier.)


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found