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 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].
Examplesopen allclose all
Basic Examples (5)
Use ReplaceAll in operator form:
Use Condition to restrict to parts that satisfy a Boolean expression:
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:
Properties & Relations (7)
Use iterated calls to ReplaceAll to apply each rule to all parts:
ReplaceAll replaces any matching part:
Replace replaces a whole expression by default:
ReplaceAll replaces the largest subexpressions it can and then stops:
ReplaceAll replaces parts of expressions that match a pattern:
ReplacePart replaces parts of expressions whose positions match a pattern:
Possible Issues (4)
Using Dispatch can be significantly faster:
Associations can be used to specify replacement rules, but the keys are treated as Verbatim values:
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).
Wolfram Language. 1988. "ReplaceAll." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/ReplaceAll.html.
Wolfram Language. (1988). ReplaceAll. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceAll.html