Finding Expressions That Match a Pattern

Cases[list,form]give the elements of list that match form
Count[list,form]give the number of elements in list that match form
Position[list,form,{1}]give the positions of elements in list that match form
Select[list,test]give the elements of list on which test gives True
Pick[list,sel,form]give the elements of list for which the corresponding elements of sel match form

Finding elements that match a pattern.

This gives the elements of the list which match the pattern .
In[1]:=
Click for copyable input
Out[1]=
Here is the total number of elements which match the pattern.
In[2]:=
Click for copyable input
Out[2]=

You can apply functions like Cases not only to lists, but to expressions of any kind. In addition, you can specify the level of parts at which you want to look.

Cases[expr,lhs->rhs]find elements of expr that match lhs, and give a list of the results of applying the transformation rule to them
Cases[expr,lhs->rhs,lev]test parts of expr at levels specified by lev
Count[expr,form,lev]give the total number of parts that match form at levels specified by lev
Position[expr,form,lev]give the positions of parts that match form at levels specified by lev

Searching for parts of expressions that match a pattern.

This returns a list of the exponents .
In[3]:=
Click for copyable input
Out[3]=
The pattern matches any integer. This gives a list of integers appearing at any level.
In[4]:=
Click for copyable input
Out[4]=
Cases[expr,form,lev,n]find only the first n parts that match form
Position[expr,form,lev,n]give the positions of the first n parts that match form

Limiting the number of parts to search for.

This gives the positions of the first two powers of appearing at any level.
In[5]:=
Click for copyable input
Out[5]=
The positions are specified in exactly the form used by functions such as Extract and ReplacePart discussed in "Lists".
In[6]:=
Click for copyable input
Out[6]=
DeleteCases[expr,form]delete elements of expr that match form
DeleteCases[expr,form,lev]delete parts of expr that match form at levels specified by lev

Deleting parts of expressions that match a pattern.

This deletes the elements which match .
In[7]:=
Click for copyable input
Out[7]=
This deletes all integers appearing at any level.
In[8]:=
Click for copyable input
Out[8]=
ReplaceList[expr,lhs->rhs]find all ways that expr can match lhs

Finding arrangements of an expression that match a pattern.

This finds all ways that the sum can be written in two parts.
In[9]:=
Click for copyable input
Out[9]=
This finds all pairs of identical elements. The pattern stands for any sequence of elements.
In[10]:=
Click for copyable input
Out[10]=