in reply to Regex with multiple pattern omissions

A slightly different approach occurred to me. The alternation in the code below 'looks for' (and steps over) everything, even the stuff you want to ignore, but only returns (as a list) those patterns that are captured. The items to be ignored must be first in the alternation! Use of capture groups in an alternation has the side-effect of producing a bunch of undefined list items because every capture group always produces an output even if the output is undefined because the group was not 'visited' in the alternation. This is easily dealt with by grepping for defined values.

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;

Output:

'"non$volatile display" and ((timer oR count$3 Or display) near5 hour) +.ccls. NOT (LCD).ab.' '5' 'LCD' 'count$3' 'display' 'hour' 'non$volatile display' 'timer'