my $office = 'boston'; #office to match on my @matched; open(EMPLOYEES, 'employees.txt'); while(){ chomp; if(/(.*?)\s*(\S*@\S*)\s*(.*$office.*)/i){ push @matched, "$1 $2"; } } print "Employees from $office office:\n"; foreach(@matched){ print "$_\n"; }