http://www.perlmonks.org?node_id=576352


in reply to regex for negating a sequence

I know this is outside the scope of the original question but this is a good opportunity to try your hand at some testing! Sometimes a regexp might look like it will do the job but how can you be SURE it does what you expect?

Try the following code:

use Test::Simple tests => 14; use strict; # # PUT THE REGEXP TO TEST HERE # my $testregexp = qr/\x26\x67(?!.{0,6}\x26\x67).{8}/s; sub mytest( $ ) { return( ( $_[0] =~ m/$testregexp/ ) ? 1 : 0 ); } # tests ok( mytest( "\x26\x67________" ), "lead bytes followed by 8 clear bytes" ); ok( !mytest( "\x26\x67_______" ), "lead bytes followed by only 7 clear bytes" ); ok( mytest( "\x26\x67_________" ), "lead bytes followed by 9 clear bytes" ); ok( !mytest( "\x26\x67\x26\x67______" ), "lead bytes with 2 bad bytes" ); ok( !mytest( "\x26\x67_\x26\x67_____" ), "lead bytes with 2 bad bytes" ); ok( !mytest( "\x26\x67_____\x26\x67_" ), "lead bytes with 2 bad bytes" ); ok( !mytest( "\x26\x67______\x26\x67" ), "lead bytes with 2 bad bytes" ); ok( mytest( "\x26\x67________\x26\x67" ), "lead bytes with 1 bad byte inside the eight byte limit" ); ok( mytest( "\x26\x67_______\x26\x67" ), "lead bytes with 1 bad bytes" ); ok( !mytest( "\x26\x67\x26\x67\x26\x67____" ), "lead bytes with 2 sets of bad bytes" ); ok( !mytest( "\x26\x67\x26\x67_\x26\x67___" ), "lead bytes with 2 sets of bad bytes" ); ok( !mytest( "\x26\x67_\x26\x67\x26\x67___" ), "lead bytes with 2 sets of bad bytes" ); ok( !mytest( "\x26\x67_\x26\x67___\x26\x67" ), "lead bytes with 2 sets of bad bytes" ); ok( !mytest( "\x26\x67\x26\x67____\x26\x67" ), "lead bytes with 2 sets of bad bytes" );

You can see that I've tried to cover all the cases I can think of in your requirements. Do they look right? Do the test situations describe all the match/nomatch situations you're expecting?

The test script produces the following output:

C:\temp>perl -w regexp.t 1..14 ok 1 - lead bytes followed by 8 clear bytes ok 2 - lead bytes followed by only 7 clear bytes ok 3 - lead bytes followed by 9 clear bytes ok 4 - lead bytes with 2 bad bytes ok 5 - lead bytes with 2 bad bytes ok 6 - lead bytes with 2 bad bytes ok 7 - lead bytes with 2 bad bytes ok 8 - lead bytes with 1 bad byte inside the eight byte limit ok 9 - lead bytes with 1 bad bytes ok 10 - lead bytes with 2 sets of bad bytes ok 11 - lead bytes with 2 sets of bad bytes ok 12 - lead bytes with 2 sets of bad bytes ok 13 - lead bytes with 2 sets of bad bytes ok 14 - lead bytes with 2 sets of bad bytes

You can plug in anybody's suggestion into the script and verify that it does, indeed, do what you want. If you get the hang of a little testing now you'll find it easier for other things too! Good luck!