This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
 MATHEMATICA TUTORIAL Related Tutorials »| Functions »

# 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 .
 Out[1]=
Here is the total number of elements which match the pattern.
 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 .
 Out[3]=
The pattern matches any integer. This gives a list of integers appearing at any level.
 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.
 Out[5]=
The positions are specified in exactly the form used by functions such as Extract and ReplacePart discussed in "Lists".
 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 .
 Out[7]=
This deletes all integers appearing at any level.
 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.
 Out[9]=
This finds all pairs of identical elements. The pattern stands for any sequence of elements.
 Out[10]=