Question: How to find all occurrences of a pattern in an expression?

I thought I remembed how to do this once in Maple, or asking something like this here, but may be it was something similar. But I am not able to figure it now or remember.

Given an expression, I want to find all occuranes of a pattern in it.  Not just one.  So this is like using patmatch but over and over again untill all patterns found. I'll give an example to make it easy to explain.


expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;

I want to find all patterns of form y^n  so the result should be 

{y^(3/2), y^7, y^2, 1/y, y}

This below is how it is done in Mathematica, but having hard time translating this code to Maple.

The last line below does the actual repeated pattern matching. That line was not written by me. It is something from Mathematica forum at stackexchange and I use it all the time and it works well.

expr = y^2*Sin[1/y] + y^(3/2) + y + x*y^7;
pat = y^n_.;
Last@Reap[expr /. a : pat :> Sow[a], _, Sequence @@ #2 &]

I looked at hastype also. But hastype will only tell me if the pattern is there or not. May be I did not use it right.

expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;

Gives true

I tried patmatch, but again, it only find one:

expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;
if patmatch(expr,a::anything+y^(n::anything)+b::anything,'la') then

And the above is not even robust for finding one. Since I have to change the pattern again to account for multiplication/addition cases between terms. 

Is it possible to do in Maple the same thing I show in Mathematica? I am sure it is possible, but can't now find the right function in Maple.

Maple 2019.1

Please Wait...