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'

In reply to Re: Regex with multiple pattern omissions by AnomalousMonk
in thread Regex with multiple pattern omissions by jhoop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.