while( $input =~ m{\G(?:.{$n})*?(?=fred(....))}g ) { pos($input) += $n; # ... # do stuff with $1 # ... }