perl -le 'print for map { /ab(?!ab)(.(?!ab)){2}.{2}/s ? $& : () } @ARGV' abhjabdf abasdasd abdasdadsab ababblah OUTPUTS: abasda abdasd abblah