1
$\begingroup$

I am trying match variables with subscripts just returning true or false depending if pattern is matched. My try is to use Cases. However the following does not return a match

Cases[Subscript[x, 2], Subscript[x, l_] /; l > 1]

Moreover, if this would work there must something nicer than

Length[    Cases[Subscript[x, 2], Subscript[x, l_] /; l > 1]]>=1
$\endgroup$
  • $\begingroup$ Cases[{Subscript[x, 2]}, Subscript[x, l_] /; l > 1] , MatchQ[Subscript[x, 2], Subscript[x, l_] /; l > 1]for just single test. $\endgroup$ – ciao Mar 26 '14 at 21:46
  • $\begingroup$ MatchQ[( Subscript[x, 2])^2, _ Subscript[x, l_] _ /; l > 1] does not work, I want to check if it occurs anywhere in the term i.e. would the rule Subscript[x, l_] _ /; l > 1:> 5 change something $\endgroup$ – warsaga Mar 26 '14 at 22:05
  • $\begingroup$ That's because your use of MatchQ there is a bit wacky. Perhaps clarify more precisely what you want in the OP? It seems perhaps Cases[Level[expression, Infinity], Subscript[x, l_] /; l > 1] might be what you're after. $\endgroup$ – ciao Mar 26 '14 at 23:05
  • $\begingroup$ Not@FreeQ[(Subscript[x, 2])^2, Subscript[x, l_] /; l > 1]? $\endgroup$ – kglr Mar 27 '14 at 0:08
1
$\begingroup$

As mentioned in the comments, you can use either MatchQ:

MatchQ[Subscript[x, 2], Subscript[x, _?(# > 1 &)]]

or FreeQ with Not:

!FreeQ[Subscript[x, 2], Subscript[x, _?(# > 1 &)]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.