Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Previous section-----Next section

2.3.2 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 x^_.

In[1]:=  Cases[ {3, 4, x, x^2, x^3}, x^_ ]

Out[1]=

Here is the total number of elements which match the pattern.

In[2]:=  Count[ {3, 4, x, x^2, x^3}, x^_ ]

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 n.

In[3]:=  Cases[ {3, 4, x, x^2, x^3}, x^n_ -> n]

Out[3]=

The pattern _Integer matches any integer. This gives a list of integers appearing at any level.

In[4]:=  Cases[ {3, 4, x, x^2, x^3}, _Integer, Infinity]

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 x appearing at any level.

In[5]:=  Position[ {4, 4 + x^a, x^b, 6 + x^5}, x^_, Infinity, 2]

Out[5]=

The positions are specified in exactly the form used by functions such as Extract and ReplacePart discussed in Section 1.8.

In[6]:=  ReplacePart[ {4, 4 + x^a, x^b, 6 + x^5}, zzz, % ]

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 x^n_.

In[7]:=  DeleteCases[ {3, 4, x, x^2, x^3}, x^n_ ]

Out[7]=

This deletes all integers appearing at any level.

In[8]:=  DeleteCases[ {3, 4, x, 2+x, 3+x}, _Integer, Infinity ]

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]:=  ReplaceList[a + b + c, x_ + y_ -> g[x, y]]

Out[9]=

This finds all pairs of identical elements. The pattern ___ stands for any sequence of elements.

In[10]:=  ReplaceList[{a, b, b, b, c, c, a},
{___, x_, x_, ___} -> x]

Out[10]=


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2009 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: