ReplaceAll

expr/.rules or 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:

Wolfram Research (1988), ReplaceAll, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAll.html (updated 2015).

Text

Wolfram Research (1988), ReplaceAll, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAll.html (updated 2015).

CMS

Wolfram Language. 1988. "ReplaceAll." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/ReplaceAll.html.

APA

Wolfram Language. (1988). ReplaceAll. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceAll.html

BibTeX

@misc{reference.wolfram_2024_replaceall, author="Wolfram Research", title="{ReplaceAll}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ReplaceAll.html}", note=[Accessed: 24-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_replaceall, organization={Wolfram Research}, title={ReplaceAll}, year={2015}, url={https://reference.wolfram.com/language/ref/ReplaceAll.html}, note=[Accessed: 24-November-2024 ]}