ReplaceRepeated

expr//.rules
repeatedly performs replacements until expr no longer changes.

Details and OptionsDetails and Options

  • expr//.rules effectively applies /. repeatedly, until the results it gets no longer change.
  • It performs one complete pass over the expression using /., then carries out the next pass.
  • You should be very careful to avoid infinite loops when you use the //. operator. The command x//.x->x+1 will, for example, lead to an infinite loop.
  • ReplaceRepeated takes the option MaxIterations, which specifies the maximum number of times it will try to apply the rules you give. The default setting is MaxIterations->65536. With MaxIterations->Infinity there is no limit.

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

Apply rules for the power and product laws for logarithms of real numbers recursively:

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

ReplaceAll does just a single replacement:

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

Remove all occurrences of a head, including nested ones:

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

ReplaceAll would only remove the outermost head:

In[2]:=
Click for copyable input
Out[2]=
Introduced in 1988
(1.0)