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:
Output (same for ActiveState 5.8.2 and Strawberry 5.10.0.5):# 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;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |