6
$\begingroup$

I know I have seen this before but can't seem to locate it today.

I am trying to take an expression that has some rules in it and replace one of that parts that contain a rule using ReplaceAll

For a simple example consider:

testExpression = {"stuff", Axes -> {False, True}}
(* {"stuff", Axes -> {False, True}} *)

and I want to replace the

Axes -> {False, True}

with

Axes -> False

I have tried

testExpression /. 
 Hold[Axes -> {False, True}] -> Hold[Axes -> False]
(* {"stuff", Axes -> {False, True}} *)

as well as

testExpression /. 
 Hold[Rule[Axes , {False, True}]] -> Hold[Rule[Axes , False]]
(* {"stuff", Axes -> {False, True}} *)

I promise to add this to my set of notes if someone could kindly enlighten me.

$\endgroup$
8
$\begingroup$

Here are a couple possibilities:

testExpression /. Rule[Axes, {False, True}] -> Axes -> False
testExpression /. HoldPattern[Axes -> {False, True}] -> Axes -> False

{"stuff", Axes -> False}

{"stuff", Axes -> False}

$\endgroup$
6
$\begingroup$

I think this is just a matter of getting the parentheses right

testExpression /. (Axes -> {False, True}) -> (Axes -> False)
(* {"stuff", Axes -> False} *)
$\endgroup$
  • $\begingroup$ I'd have posted this too. (+1) $\endgroup$ – J. M. will be back soon Mar 31 at 22:21
  • $\begingroup$ Wish I could accept them both $\endgroup$ – Jack LaVigne Apr 1 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.