That won't work if the term is longer than one character...
True, true.

Actually, my preference is for an approach like that of Re: Building a boolean search engine. It's more wordy, but also provides more flexibility and control.
(Update: But I guess one could ask: If you're going to do all that, why not just write a recursive descent parser?)

So, something like:

# use feature ':5.10'; use strict; use warnings; my $str = "In this example, AA plus B equals C, D minus EE times FFF equals G and HH plus I times JJ minus K equals L and M plusplus N plus plus O is invalid and P equals Q and RRR plus T"; use constant OPS => qw(plus minus times); my $op = qr{ \b (?: @{[ join '|', map quotemeta, OPS ]} ) \b }xms; my $not_op = qr{ (?! $op) }xms; my $operand = qr{ \b (?: $not_op \w)+ \b }xms; my $term = qr{ $operand \s+ $op \s+ $operand }xms; my @terms = $str =~ m{ (?= ($term)) }xmsg; print "$_ \n" for @terms;
Output (same for ActiveState 5.8.2 and Strawberry 5.10.0.5):
AA plus B D minus EE EE times FFF HH plus I I times JJ JJ minus K RRR plus T

In reply to Re^4: Regular Expression rematch by AnomalousMonk
in thread Regular Expression rematch by dlw

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.