0

Given the examplary expression

expr = (x1 + x17 y2 )/Sqrt[y1 + xx3]

I want to apply a rule for every symbol-name starting with "x".

My solution

expr /. x_ :>f[x] /; And[Head[x] == Symbol, StringTake[ToString[x], 1] =="x"]
(* (f[x1] + y2 f[x17])/Sqrt[y1 + f[xx3]] *)

works but seems to be very complicated.

How could I simplify my approach? Thanks.

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
  • related: https://mathematica.stackexchange.com/q/8427/5478 – Kuba Mar 08 '18 at 09:19
  • Found a better one, let me know if you disagree with closing. – Kuba Mar 08 '18 at 09:24
  • @Kuba: I disagree but accept closing. The links all show solutions using string operations in more or less complicated ways. I'm still wondering that MMA has no direct pattern-solution. – Ulrich Neumann Mar 08 '18 at 09:41
  • @kuba: where can I find the exact duplicate of my question? – Ulrich Neumann Mar 08 '18 at 09:43
  • Link in the top of this question. Linked question is the same except of what is the result of replacement, they have ->3 you have :>f[x]. The fact there isn't anything shorter there suggests that is the way to go. Unless you want to see an answer like: "no, go with linked topic". – Kuba Mar 08 '18 at 09:43

0 Answers0