my @searchTexts = split (/and|or/i,$query); foreach my $text (@searchtexts) { $text =~ s/^\s+//; $text =~ s/\s+$//; print "$text\n"; }