Pattern matching multiple symbols names and powers
I believe that if you want to match a Symbol by its constituent characters that you do need to use string patterns, therefore your solution is reasonable. I recommend a variation:
p2 = s_Symbol^_. /; StringMatchQ[SymbolName[s], "V*"]
s_Symbol
makes sure that the expression that is passed to SymbolName
is actually a Symbol. I also find it somewhat more readable. Test:
MatchQ[p2] /@ {V1, V1^2, V2, x^2, y} (* v10 operator form syntax *)
{True, True, True, False, False}
The use of Condition
rather than PatternTest
makes it easier if you may be working with held expressions and need to add Unevaluated
:
p3 = s_Symbol^_. /; StringMatchQ[SymbolName[Unevaluated@s], "V*"]
This allows:
V1 = "Fail!";
Cases[Hold[V1, V1^2, V2, x^2, y], x : p2 :> Hold[x]]
V1 =.;
{Hold[V1], Hold[V1^2], Hold[V2]}
To make the code a bit prettier consider an abstraction such as:
symPat = s_Symbol /; StringMatchQ[SymbolName[Unevaluated@s], #] &;
Then simply:
Cases[{V1, V1^2, V2, x^2, y}, symPat["V*"]^_.]
{V1, V1^2, V2}