i think this will work for you:
/\x26\x67(?!\x26\x67)(.(?!\x26\x67)){6}.{2}/s
Based upon this test code to try to match ab followed by 4 bytes w/no ab sequence:
perl -le 'print for map { /ab(?!ab)(.(?!ab)){2}.{2}/s ? $& : () } @AR
+GV' abhjabdf abasdasd abdasdadsab ababblah
OUTPUTS:
abasda
abdasd
abblah
Update: Here's a commented version:
/
\x26\x67 # Starting sequence (let's call it AB)
(?!\x26\x67) # Exclude double starting seqeuence, e.g. ABAB
(?: # non-capture
.(?!\x26\x67) # Any character that's not followed by the sequenc
+e
){6} # Need N-2 of these
.{2} # And the last two characters can be anything.
# note we know they're not AB because otherwise the
# (N-2)th item in the previous part wouldn't have mat
+ched
/sx # Enable comments, and make . mean everything
Update: Here's a generic version:
my $marker = '&g';
my $N = 8;
my $n1 = $N-2;
/$marker(?!$marker)(?:.(?!$marker)){$n1}.{2}/s
Update: (As pointed out by
diotalevi) Added the
/s modifier in case the data stream can have a newline in it.