use warnings; use strict; use List::MoreUtils qw(uniq); # extract these. my $d_quoted = qr{ [^"]* }xms; # body of "-quoted sub-string my $searchterms = qr{ [[:alnum:]\$]+ }xms; # ignore these. my $dotted = qr{ \. [[:alpha:]]+ \. }xms; my $control = qr{ terms | and | or | not | with | near | same | xor | adj }xmsi; # note /i case insensitive my $ignore = qr{ $dotted | $control }xms; my $test = q{"non$volatile display" and ((timer oR count$3 Or } . q{display) near5 hour).ccls. NOT (LCD).ab.}; my @output = sort uniq grep { defined } $test =~ m{ $ignore | ($searchterms) | " ($d_quoted) " }xmsg ; print qq{'$test' \n}; print qq{'$_' } for @output;