use strict; use warnings; use re 'eval'; my $find = qr/123/; while ( ) { chomp; if ( m/$find(?(?{(pos()<=length($_)*.5)})|\w\b\w)/ ) { print "$_ matched.\n"; } else { print "$_ didn't match.\n"; } } __DATA__ 1230000000 0123000000 0012300000 0001230000 0000123000 0000012300 0000001230 0000000123