How to extract all possible sub-products which match specific pattern?
To get all the different ways this pattern can match, we can use ReplaceList
, since none of Cases
, Replace
, ReplaceAll
tries to match the same part more than once. Here is one way:
ReplaceList[expr,
d_. + Times[c___, Dot[a_, b_], f[x_]] :> Dot[a, b] f[x]]
{a.b f[x], c.d f[x], m.n f[y], p.q f[y], r.s f[y]}
The reason I'm writing Dot[a_, b_]
and not just a_.b_
is that looking at the FullForm
of what I'm trying to match always seems to help me figure out a good pattern to use :)
You can also use SequenceCases
with the option Overlaps -> All
:
SequenceCases[List @@ expr, { _. a_Dot b_f} :> a b, Overlaps -> All]
{c.d f[x], a.b f[x], r.s f[y], p.q f[y], m.n f[y]}