Mathematica 9 is now available
 Documentation / Mathematica / Built-in Functions / Programming / Rule Application  /
ReplaceAll

  • expr /. rules applies a rule or list of rules in an attempt to transform each subpart of an expression expr.
  • Example: x + 2 /. x -> a.
  • 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.
  • Example: x /. x -> x + 1.
  • See the notes on Replace for a description of how rules are applied to each part of expr.
  • expr /. rules returns expr if none of the rules apply.
  • See the Mathematica book: Section 2.4.1.
  • See also: Rule, Set, MapAll.

    Further Examples

    ReplaceAll replaces once at all levels of the expression.

    In[1]:=

    Out[1]=

    On the other hand, Replace applies only to the whole expression.

    In[2]:=

    Out[2]=

    In[3]:=

    Out[3]=

    ReplaceAll applies the set of rules exactly once on each part of the expression.

    In[4]:=

    Out[4]=

    ReplaceRepeated uses ReplaceAll repeatedly until the expression stops changing.

    In[5]:=

    Out[5]=



    Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
    THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
    SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.