my $last4 = ''; my $buffer = ''; my $find = qr /1234/; my $chunk = 7; # 2**20 or some such is more logical! while ( read ( DATA, $buffer, $chunk ) ) { print "got last-$last4\tbuffer-$buffer\n"; print "simple match\n" while $buffer =~ s/$find//; $buffer = $last4 . $buffer; $last4 = substr $buffer, -4, 4, ''; print "buffer match\n" if $buffer =~ m/$find/; } __DATA__ 1234567890123456789012345678901234567890123456789012345678901234567890 __END__ got last- buffer-1234567 simple match got last-567 buffer-8901234 simple match got last-7890 buffer-5678901 got last-8901 buffer-2345678 buffer match got last-5678 buffer-9012345 simple match got last-8905 buffer-6789012 got last-9012 buffer-3456789 buffer match got last-6789 buffer-0123456 simple match got last-9056 buffer-7890123 got last-0123 buffer-4567890 buffer match got last-7890 buffer- #### my $last4 = ''; my $buffer = ''; my $find = '1234'; my $find_re = qr /$find/; my $length = length $find; my $chunk = 7; # 2**20 or some such is more logical! my $pos = 0; while ( my $read_length = read ( DATA, $buffer, $chunk ) ) { $buffer = $last4 . $buffer; print "got last-$last4\tbuffer-$buffer\n"; while ($buffer =~ m/$find_re/g) { print "match at ", $pos - (length $last4) -$length + pos $buffer, "\n"; } $last4 = substr $buffer, -$length, $length, ''; $last4 = '' if $last4 =~ m/$find_re/; # stop double match bug $pos += $read_length; } __DATA__ 1234567890123456789012345678901234567890123456789012345678901234567890