# Obtain the values of all Sub- and/or Superscripts from an expression?

Let Expr be an expression that can be practically any combination of variables. Now I can check whether this expression contains any subscripts or superscripts with help of

!FreeQ[Expr,Subscript]
!FreeQ[Expr,Superscript]


However, in a next step I would like to read out a list of all appearing sub- and superscripts explicitly without introducing more constraints on how the expression should look like specifically. Is there any function that does this? Or maybe one can implement it? Thanks for any suggestion!

Example:

Expr = (h*Superscript[u, g]*Superscript[Subscript[a, b, c], z])^Pi;
GetSubAndSuperscripts[Expr]


{g,z,b,c}

The point is, that it should not matter how exactly the expression is organized. The function should simply return all appearing Superscripts and Subscripts.

• Not sure what you want. Would Cases do? Nov 26 '14 at 21:52
• No, I'm afraid Cases is too specific. I will add an example to the question to make it more clear. Nov 26 '14 at 21:56

GetSubAndSuperscripts[expr_] :=