regular expression negate a word (not character)
Dr.Ruud
rvtol+news at isolution.nl
Mon Jan 28 15:00:55 EST 2008
Greg Bacon schreef:
> #! /usr/bin/perl
>
> use warnings;
> use strict;
>
> use constant {
> MATCH => 1,
> NO_MATCH => 0,
> };
>
> my @tests = (
> [ "winter tire", => MATCH ],
> [ "tire", => MATCH ],
> [ "retire", => MATCH ],
> [ "tired", => MATCH ],
> [ "snowbird tire", => MATCH ],
> [ "tired on a snow day", => MATCH ],
> [ "snow tire and regular tire", => MATCH ],
> [ " tire" => MATCH ],
> [ "snow tire" => NO_MATCH ],
> [ "snow tire" => NO_MATCH ],
> [ "some snowtires" => NO_MATCH ],
> );
> [...]
I negated the test, to make the regex simpler:
my $snow_tire = qr/
snow [[:blank:]]* tire (?!.*tire)
/x;
my $fail;
for (@tests) {
my($str,$want) = @$_;
my $got = $str !~ /$snow_tire/;
my $pass = !!$want == !!$got;
print "$str: ", ($pass ? "PASS" : "FAIL"), "\n";
++$fail unless $pass;
}
print "\n", (!$fail ? "PASS" : "FAIL"), "\n";
__END__
--
Affijn, Ruud
"Gewoon is een tijger."
More information about the Python-list
mailing list