Applying Transformation Rules

expr/.lhs->rhsapply a transformation rule to expr
expr/.{lhs1->rhs1,lhs2->rhs2,}try a sequence of rules on each part of expr

Applying transformation rules.

The replacement operator (pronounced "slashdot") applies rules to expressions.
In[1]:=
Click for copyable input
Out[1]=
You can give a list of rules to apply. Each rule will be tried once on each part of the expression.
In[2]:=
Click for copyable input
Out[2]=
expr/.{rules1,rules2,}give a list of the results from applying each of the to expr

Applying lists of transformation rules.

If you give a list of lists of rules, you get a list of results.
In[3]:=
Click for copyable input
Out[3]=
Functions such as Solve and NSolve return lists whose elements are lists of rules, each representing a solution.
In[4]:=
Click for copyable input
Out[4]=
When you apply these rules, you get a list of results, one corresponding to each solution.
In[5]:=
Click for copyable input
Out[5]=

When you use , each rule is tried in turn on each part of expr. As soon as a rule applies, the appropriate transformation is made, and the resulting part is returned.

The rule for is tried first; if it does not apply, the rule for is used.
In[6]:=
Click for copyable input
Out[6]=
A result is returned as soon as the rule has been applied, so the inner instance of is not replaced.
In[7]:=
Click for copyable input
Out[7]=

The replacement tries each rule just once on each part of expr.

Since each rule is tried just once, this serves to swap and .
In[8]:=
Click for copyable input
Out[8]=
You can use this notation to apply one set of rules, followed by another.
In[9]:=
Click for copyable input
Out[9]=

Sometimes you may need to go on applying rules over and over again, until the expression you are working on no longer changes. You can do this using the repeated replacement operation (or ReplaceRepeated[expr,rules]).

expr/.rulestry rules once on each part of expr
expr//.rulestry rules repeatedly until the result no longer changes

Single and repeated rule application.

With the single replacement operator each rule is tried only once on each part of the expression.
In[10]:=
Click for copyable input
Out[10]=
With the repeated replacement operator the rules are tried repeatedly until the expression no longer changes.
In[11]:=
Click for copyable input
Out[11]=
Here the rule is applied only once.
In[12]:=
Click for copyable input
Out[12]=
With the repeated replacement operator, the rule is applied repeatedly, until the result no longer changes.
In[13]:=
Click for copyable input
Out[13]=

When you use (pronounced "slashslashdot"), the Wolfram Language repeatedly passes through your expression, trying each of the rules given. It goes on doing this until it gets the same result on two successive passes.

If you give a set of rules that is circular, then can keep on getting different results forever. In practice, the maximum number of passes that makes on a particular expression is determined by the setting for the option MaxIterations. If you want to keep going for as long as possible, you can use ReplaceRepeated[expr,rules,MaxIterations->Infinity]. You can always stop by explicitly interrupting the Wolfram Language.

By setting the option MaxIterations, you can explicitly tell ReplaceRepeated how many times to try the rules you give.
In[14]:=
Click for copyable input
Out[14]=

The replacement operators and share the feature that they try each rule on every subpart of your expression. On the other hand, Replace[expr,rules] tries the rules only on the whole of expr, and not on any of its subparts.

You can use Replace, together with functions like Map and MapAt, to control exactly which parts of an expression a replacement is applied to. Remember that you can use the function ReplacePart[expr,new,pos] to replace part of an expression with a specific object.

The operator applies rules to all subparts of an expression.
In[15]:=
Click for copyable input
Out[15]=
Without a level specification, Replace applies rules only to the whole expression.
In[16]:=
Click for copyable input
Out[16]=
No replacement is done here.
In[17]:=
Click for copyable input
Out[17]=
This applies rules down to level 2, and so replaces .
In[18]:=
Click for copyable input
Out[18]=
expr/.rulesapply rules to all subparts of expr
Replace[expr,rules]apply rules to the whole of expr only
Replace[expr,rules,levspec]apply rules to parts of expr on levels specified by levspec

Applying rules to whole expressions.

Replace returns the result from using the first rule that applies.
In[19]:=
Click for copyable input
Out[19]=
ReplaceList gives a list of the results from every rule that applies.
In[20]:=
Click for copyable input
Out[20]=
If a single rule can be applied in several ways, ReplaceList gives a list of all the results.
In[21]:=
Click for copyable input
Out[21]=
This gives a list of ways of breaking the original list in two.
In[22]:=
Click for copyable input
Out[22]=
This finds all sublists that are flanked by the same element.
In[23]:=
Click for copyable input
Out[23]=
Replace[expr,rules]apply rules in one way only
ReplaceList[expr,rules]apply rules in all possible ways

Applying rules in one way or all possible ways.