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:

Replace a variable with a list:

Replace heads of expressions:

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

Apply the first matching rule:

Apply each rule separately:

Use ReplaceAll in operator form:

Scope  (14)

If no rule matches, the original expression is returned:

Match expressions with one argument:

Match expressions with one or more arguments:

Match expressions with zero or more arguments:

Replace expressions with a particular head:

Use PatternTest to replace parts that yield True when a test function is applied:

Use Condition to restrict to parts that satisfy a Boolean expression:

Replace a pair of pairs:

Replace each inner pair by restricting the pattern:

ReplaceAll also works with RuleDelayed:

Structurally insert into a held expression:

Do not evaluate the right-hand side of the rule before doing the replacement:

Evaluate the right-hand side before replacement:

Dispatch can be used in place of a list of rules:

Association can be used in place of a list of rules:

Use Association as a pattern for replacement:

Use KeyValuePattern to replace in a list of rules:

Extract the key from an Association that matches a rule:

Find a key in a list of rules satisfying a criterion:

Properties & Relations  (7)

An empty list is considered to have no matching rules:

When a list of lists is used for replacement, the result is a list of the same length:

Only the first rule that matches is applied to each part:

Use iterated calls to ReplaceAll to apply each rule to all parts:

Evaluation is not forced when replacing into a held expression:

ReplaceAll replaces any matching part:

Replace replaces a whole expression by default:

It also allows specific levels to be replaced:

ReplaceAll replaces the largest subexpressions it can and then stops:

ReplaceRepeated repeatedly applies ReplaceAll until the expression stops changing:

Replace with level spec All will attempt to replace every subexpression exactly once:

ReplaceAll replaces parts of expressions that match a pattern:

ReplacePart replaces parts of expressions whose positions match a pattern:

Possible Issues  (4)

Applying a long list of rules can be slow:

Using Dispatch can be significantly faster:

Associations can be used to specify replacement rules, but the keys are treated as Verbatim values:

Compare with the following:

The list of rules equivalent to the association is the following:

Periods bind to numbers more strongly than to slash, so the following is a division operation:

Insert a space between /. and numbers to avoid that issue:

Most patterns are only compared against the values of an association:

For compound patterns matching an Association, inner patterns match both keys and values:

By excluding heads matching Association, only comparisons against values are done:

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