Edit: I am matching and replacing a particular expression. At the same time... I want to store matched variables for later use. I got it to work with messed up code involving Block. Is there a smarter way to do this?
(*Initialize*) variables = {}; expr = f[x] + g[x] + 3 f[y]; (*now match and replace, and storing the argument into "variables"*) expr /. f[x_] :> Block[{}, variables = Union[variables, {x}]; fNew[x]] Now if I type:
variables (*output is {x,y}*) I get the desired output.
The empty {} inside Block makes me feel I'm doing this in a kludgy way... (feel free to improve any other parts of the code too!)
f[]orf[x,y]? $\endgroup$f[]andf[x,y]do not match. $\endgroup$f[g[x]]should match? $\endgroup$f[g[x]]that should match. $\endgroup$