use strict; use warnings; while () { print "Matched $_" if /train times (?!to\b|from\b)\w/; } __DATA__ train times to xyz train times from xyz train times including xyz train times tomorrow xyz #### Matched train times including xyz Matched train times tomorrow xyz