$num = 2; while (<>) { if ( /(\A|\D)$num(\D|\Z)/ ) { print "match\n"; } }