Skip to content

Cases

Cases[expr, pat] returns a new List of all elements in expr that match pat.

Attributes[Cases] := {Protected}

Simple examples

In[1]:= Cases[{5, 2, 3.5, x, y, 4}, _]
Out[1]= {5, 2, 3.5, x, y, 4}
In[2]:= Cases[{5, 2, 3.5, x, y, 4}, _Integer]
Out[2]= {5, 2, 4}
In[3]:= Cases[{5, 2, 3.5, x, y, 4}, _Real]
Out[3]= {3.5}
In[4]:= Cases[{b^2, 1, a^c}, _^e_ -> e]
Out[4]= {2, c}

Further examples

expr need not be a list:

In[1]:= Cases[bar[a, b, c], a]
Out[1]= {a}