| Author |
Comment/Response |
Hari Sundararajan
|
10/31/06 8:54pm
Hello
I am given an expression like this:
x[la]y[ub] + x[lqw]y[ubf]
And the result must be this:
x_a y^b + x_qw y^bf
In other words, every time a pattern of [l.*] is seen, the .* must become the subscript, and a pattern of [u.*] is seen the .* must become the superscript.
My initial reaction to this was as follows:
/. {x_[p_] /; StringMatchQ[
ToString[p], RegularExpression["u.*"]] -> Superscript[x,StringDrop[ToString[p],{1}]}
In other words, parse it, if you see any string of the form x[p], check if p is of the form l... or u... If so make whatever comes after the 'l' or 'u' the subscript or superscript of x.
But in the second case, ToString[p] returns simply p instead of using the value of p. Why? In the first instance of ToString[p] (when checking with the regular expression) it correctly uses the value of p, but in the second case it uses p itself.
URL: , |
|