Its ugly, but if grep can look to the future, this should work:
/^(((?!BZBZB).)*BZBZB){5}((?!BZBZB).)*$/
Edit - Above {5} is a variable n times in OP. GNU grep seems to be making Perl like statements using the -P option.
Perl example
use strict;
use warnings;
my @strary = (
'this is BZBZB BZBZB BZBZB and 4 BZBZB then 5 BZBZB and done',
'BZBZBBZBZBBZBZBBZBZBBZBZBBZBZBBZBZBBZBZB BZBZB BZBZB',
'BZBZBBZBZBBZBZBBZBZBBZBZB 1',
'BZBZBZBBZBZBBZBZBBZBZBBZBZBBZBZB 2',
);
my @result = grep /^(((?!BZBZB).)*BZBZB){5}((?!BZBZB).)*$/, @strary;
for (@result) {
print "Found: '$_'\n";
}
Output
Found: 'this is BZBZB BZBZB BZBZB and 4 BZBZB then 5 BZBZB and done'
Found: 'BZBZBBZBZBBZBZBBZBZBBZBZB 1'
source
share