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.

Details

  • 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].

Examples

open allclose all

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]=

Scope  (13)

Properties & Relations  (7)

Possible Issues  (4)

See Also

Replace  Rule  Set  MapAll  ReplaceRepeated  TransformationFunctions  ReplaceList

Tutorials

Introduced in 1988
(1.0)
| Updated in 2014
(10.0)