# Rejected possibility one: substr( $string, 0, length( $string ) / 2 ) =~ m/pattern/; # Rejected possibility two: my $half = length( $string ) / 2; $string =~ m/pattern.{$half}/s; #### use strict; # Because you should. use warnings; # Because you want to. use re 'eval'; # Take your life in your own hands. my $find = qr/123/; # This is what we're searching for. while ( ) { chomp; if ( m/$find # Match the substring. (?{ (pos()<=length($_)*.5) # Test to see if our match # occurred in the first half # of the string. ? '' # Yes: pass a subexpression # that can't fail. : '\w\b\w' # No: pass a subexpression # that always fails. }) (??{$^R}) # Evaluate the passed # subexpression. /x ) { print "$_ matched.\n"; } else { print "$_ didn't match.\n"; } } __DATA__ 1230000000 0123000000 0012300000 0001230000 0000123000 0000012300 0000001230 0000000123 #### 1230000000 matched. 0123000000 matched. 0012300000 matched. 0001230000 didn't match. 0000123000 didn't match. 0000012300 didn't match. 0000001230 didn't match. 0000000123 didn't match. #### 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