ReplaceAll

expr/.rules
applies a rule or list of rules in an attempt to transform each subpart of an expression expr.

ReplaceAll[rules]
represents an operator form of ReplaceAll that can be applied to an expression.

DetailsDetails

  • ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part or on any of its subparts.
  • ReplaceAll applies a particular rule only once to an expression.
  • expr/.rules returns expr if none of the rules apply.
  • ReplaceAll[rules][expr] is equivalent to ReplaceAll[expr,rules].

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

Replace a variable with a value:

In[1]:=
Click for copyable input
Out[1]=

Replace a variable with a list:

In[2]:=
Click for copyable input
Out[2]=

Replace heads of expressions:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Use a pattern to bind a variable to a matching part:

In[1]:=
Click for copyable input
Out[1]=

Apply the first matching rule:

In[1]:=
Click for copyable input
Out[1]=

Apply each rule separately:

In[2]:=
Click for copyable input
Out[2]=

Use ReplaceAll in operator form:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 1988
(1.0)
| Updated in 2014
(10.0)
Translate this page: