mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to use zero-width negative lookahead assertions to add an AND NOT logical clause to my pattern. I wish to match /aaa/ and /aaa\/aaa/ but not /aaa/aaa/ which is to say a span delimited by unescaped slashes. What I have so far matches too much:
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { # print $+{pattern},qq(\n) if ( # for now, show the whole line print if ( m, ^ (?!\x23) (?:.*?) (?<pattern> m? (?<delimiter>[/]) (?:(?:\\?+.)*?)*? \g{delimiter} ) ,x ); } __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";
However, I have not found a successful way to match /aaa/ while at the same time rejecting /aaa/aaa/
What should I append towards the end of the pattern to ensure that /aaa/aaa/aaa/ and other similar strings are not accepted by the pattern yet /aaa/ alone would be? I have tried many scores of permutations of what to tack on, but no sucess yet. The script above produces the following result:
/src/bin/oops/otehnoes if(/ok .*$/) { if(m/a good match/( { $string =~ s/[a-f]//g; # but print this line ( $string ) = ( $string =~ /(show this)/)); /but show\/this, too/ my $dontprint this = "/var/cache/dictionaries-common";
But it should produce the following instead:
if(/ok .*$/) { if(m/a good match/( { $string =~ s/[a-f]//g; # but print this line ( $string ) = ( $string =~ /(show this)/)); /but show\/this, too/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex AND NOT with zero-width negative lookahead assertion
by hippo (Archbishop) on Mar 25, 2020 at 12:13 UTC | |
by mldvx4 (Hermit) on Mar 25, 2020 at 13:34 UTC | |
by hippo (Archbishop) on Mar 25, 2020 at 13:43 UTC | |
by Your Mother (Archbishop) on Mar 25, 2020 at 14:04 UTC | |
by AnomalousMonk (Archbishop) on Mar 26, 2020 at 03:41 UTC | |
|
Re: Regex AND NOT with zero-width negative lookahead assertion
by Veltro (Hermit) on Mar 25, 2020 at 13:07 UTC | |
|
Re: Regex AND NOT with zero-width negative lookahead assertion
by Anonymous Monk on Mar 25, 2020 at 12:41 UTC | |
by cxw (Scribe) on Mar 28, 2020 at 00:24 UTC |