|
10 | 10 |
|
11 | 11 |
|
12 | 12 | MAIN: {
|
13 |
| - my @words = @ARGV; |
| 13 | + my @words = @ARGV or die "No words"; |
14 | 14 |
|
15 | 15 | my @lines = <DATA>;
|
16 | 16 | chomp @lines;
|
|
25 | 25 | my $hl_re = join( '|', @words );
|
26 | 26 | $hl_re = qr/($hl_re)/;
|
27 | 27 |
|
28 |
| - _iter( $match_and_re, $hl_re, @lines ); |
| 28 | + _iter( $match_and_re, undef, $hl_re, @lines ); |
29 | 29 | }
|
30 | 30 |
|
31 | 31 | OR: {
|
|
37 | 37 | my $hl_re = join( '|', @words );
|
38 | 38 | $hl_re = qr/($hl_re)/;
|
39 | 39 |
|
40 |
| - _iter( $match_or_re, $hl_re, @lines ); |
| 40 | + _iter( $match_or_re, undef, $hl_re, @lines ); |
| 41 | + } |
| 42 | + |
| 43 | + NOT: { |
| 44 | + say ''; |
| 45 | + say "NOT"; |
| 46 | + my ($keep, @not) = @words; |
| 47 | + my $not_re; |
| 48 | + if ( @not ) { |
| 49 | + $not_re = join( '|', @not ); |
| 50 | + $not_re = qr/$not_re/; |
| 51 | + } |
| 52 | + my $hl_re = qr/($keep)/; |
| 53 | + |
| 54 | + _iter( $keep, $not_re, $hl_re, @lines ); |
41 | 55 | }
|
42 | 56 | }
|
43 | 57 |
|
|
47 | 61 |
|
48 | 62 | sub _iter {
|
49 | 63 | my $match_re = shift;
|
| 64 | + my $not_re = shift; |
50 | 65 | my $hl_re = shift;
|
51 | 66 | my @lines = @_;
|
52 | 67 |
|
53 |
| - say "match=$match_re"; |
54 |
| - say "hlite=$hl_re"; |
| 68 | + say " match=$match_re"; |
| 69 | + say " notre=", ($not_re // 'undef'); |
| 70 | + say " hlite=$hl_re"; |
55 | 71 |
|
56 | 72 | for ( @lines ) {
|
57 | 73 | my $line = "$_";
|
58 | 74 |
|
59 |
| - if ( $line =~ /$match_re/ ) { |
| 75 | + my $match = $line =~ /$match_re/; |
| 76 | + if ( $match && defined $not_re ) { |
| 77 | + $match = $line !~ /$not_re/; |
| 78 | + } |
| 79 | + if ( $match ) { |
60 | 80 | while ( $line =~ /$hl_re/g ) {
|
61 | 81 | my $match_start = $-[0] // next;
|
62 | 82 | my $match_end = $+[0];
|
@@ -86,3 +106,5 @@ sub _iter {
|
86 | 106 | sox got no
|
87 | 107 | table and apple and penny
|
88 | 108 | table only
|
| 109 | +penny saved an apple |
| 110 | +penny sat at the table |
0 commit comments