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$
4
  • $\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, 2014 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, 2014 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, 2014 at 23:05
  • $\begingroup$ Not@FreeQ[(Subscript[x, 2])^2, Subscript[x, l_] /; l > 1]? $\endgroup$
    – kglr
    Mar 27, 2014 at 0:08

1 Answer 1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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