23
votes
22
votes
Accepted
Is this a bug in Replace?
This is not a bug. However, this is a great opportunity to shed some light on the inner workings of Replace. What happens, in short, is that it first fully ...
19
votes
Reverse Lookup: getting keys from values
One way would be to "invert" the association and then look up values in the result:
...
18
votes
Accepted
When is the righthand side of a RuleDelayed evaluated?
There seems to be a subtlety in the way delayed rules are used. Have a look at the following:
...
17
votes
Evaluating expressions by applying corresponding rules stored in another list
MapThread[ReplaceAll,{{x,x^2,x^3},{x->1,x->2,x->3}}]
14
votes
Accepted
A simple problem about a Rule example
Pattern matching is performed based on the form of an expression, not its (mathematical) meaning. Using jargon, pattern matching is performed syntactically, not semantically.
Specifically, here ...
13
votes
Accepted
Pattern matching Association in rules
The problem can be seen in simpler expressions:
MatchQ[<||>, <||>]
(* True *)
MatchQ[<||>, HoldPattern[<||>]]
(* False *)
This behaviour ...
12
votes
Pattern Matching for Rules
Under the Possible Issues tab of the Cases documentation.
Use HoldPattern to treat the rule itself as a pattern:
...
12
votes
Accepted
I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}
DeleteCases does not by default operate on heads. You can set the Heads option to True to ...
12
votes
Accepted
11
votes
Accepted
Using two rules for the same variable
As far as I understand it, once a subexpression gets replaced, it can't get replaced again. Try this:
a*b /. {a -> a, a -> 1, b -> c}
resulting in
<...
11
votes
Why and how does RuleDelayed evaluate to itself?
RuleDelayed is unconventional in that it strips any number of occurrences of Unevaluated from the right-hand side:
...
11
votes
Can't understand one paragraph in Power Programming with Mathematica
He is simply saying that the first argument of Set is not evaluated before Set creates the definition.
However, the sub-parts ...
11
votes
Accepted
11
votes
Accepted
StringReplace with multiple patterns
Short Version
Order matters when specifying replacement rules. Rules are tried from left-to-right. Each rule will attempt to match and replace as much of the string as possible before moving on to ...
10
votes
10
votes
Reverse Lookup: getting keys from values
You may use Position on an Association to return the Key (or keys) that holds a value.
With ...
10
votes
10
votes
Pattern matching Association in rules
To @WReach's examples, one can add these:
MatchQ[<||>, Unevaluated@<||>]
MatchQ[<||>, HoldPattern[Evaluate@<||>]]
(*
False
True
*)
It ...
10
votes
Accepted
Joining all the keys of a list of rules that point to matching values
There are many ways, here's one:
KeyValueMap[#2 -> # &] @ GroupBy[rules, Last -> First, Flatten]

Kuba♦
- 136k
10
votes
Accepted
How can I get new random values with each blank in ReplaceAll?
You can simply change the Rule -> to RuleDelayed :> to give
...
10
votes
Accepted
Delete zeros from a sparse array
You can just apply SparseArray to a SparseArray object to normalize it. For your example:
...
10
votes
StringReplace with multiple patterns
WReach covered this well, but in supplement consider using the third parameter of StringReplace to see how a replacement evolves:
...
10
votes
Extract all Rules from Plot function
You can get the options associated with the plot theme "Detailed" using the (undocumented) internal function ...
10
votes
Evaluating expressions by applying corresponding rules stored in another list
I would use MapThread as it is the function designed for this purpose; but here is another solution step by step:
...
9
votes
Accepted
How to change symbols in differential equation with a single rule?
Your question suggests that you are not aware that f[x] and f'[x] have very different internal representations.
...
9
votes
Accepted
How to prevent substitution in a part of an expression?
This is IMO one of the cases where local rules are not a good match for the problem, and one is better off using custom expression parsers. The problem with rules is that the whole expression is ...
9
votes
How do I encode this replacement rule?
Function - just feed it the 5 x 5 array, returns updated array:
...
9
votes
Accepted
9
votes
Is there a simple way to apply a function to the RHS of a substitution?
Additional alternatives:
You can use MapAt to map myFunction at positions {All, -1, -1}:
<...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rule × 342replacement × 184
pattern-matching × 55
list-manipulation × 53
simplifying-expressions × 23
functions × 18
associations × 16
expression-manipulation × 15
evaluation × 14
equation-solving × 13
syntax × 12
string-manipulation × 11
assignment × 10
calculus-and-analysis × 9
symbolic × 8
plotting × 7
differential-equations × 7
function-construction × 7
matrix × 6
programming × 6
summation × 6
core-language × 6
scoping × 6
argument-patterns × 6
filtering × 5