This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Rule Application  /
Replace

  • Replace[ expr , rules ] applies a rule or list of rules in an attempt to transform the entire expression expr.
  • Example: Replace[x^2, x^2 -> a].
  • Replace[x + 1, x -> a].
  • The rules must be of the form lhs -> rhs or lhs :> rhs.
  • A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned. If none of the rules apply, the original expr is returned.
  • If the rules are given in nested lists, Replace is effectively mapped onto the inner lists. Thus Replace[ expr , , , , ... , ... ] is equivalent to Replace[ expr , , ], Replace[ expr , , ... ], ... .
  • Delayed rules defined with :> can contain /; conditions.
  • Replace[ expr , rules ] applies rules only to the complete expression expr. You can use ReplaceAll to apply rules separately to each part of an expression.
  • See the Mathematica book: Section 2.4.1.
  • See also: Rule, Set, ReplacePart, ReplaceList, StringReplace, PolynomialReduce.

    Further Examples

    See the Further Examples for ReplaceAll.