I have a text file which part of it is displayed below. Example RENTAL NOTICE LETTER EXHIBIT 10.2 FIRST RESTATED AND AMENDED AUTO LEASE NOTICE THIS LEASE is made as of the 1st day of March, 2001, by and between HANNAH RENTAL, CO. a Maine Corporation having its principal offices in Portland End What I want is to extract any phrase that contains the word "NOTICE". In this example, both "RENTAL NOTICE LETTER" and "AUTO LEASE NOTICE" are the information that I need. Because there can be two ways for locating the phrase "XXX NOTICE YYY": (1) (boundary or begining of line) XXX NOTICE YYY (\n); (2) (more than two spaces, such as " ") XXX NOTICE YYY (more than two spaces, such as " " ); I tried with the following: if ($a =~ /(?:\b| +?)(.+)*(NOTICE)(.+)*(?:\n| +?)/i) { print $1." ".$2." ".$3."\n"; }