# At what level does ReplaceAll pattern match for rule execution

**URL:** <https://community.wolfram.com/t/at-what-level-does-replaceall-pattern-match-for-rule-execution/3050>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [August 21, 2014, 8:37pm UTC](https://community.wolfram.com/t/at-what-level-does-replaceall-pattern-match-for-rule-execution/3050 "2014-08-21T20:37:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![winkllj](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@winkllj](https://community.wolfram.com/u/winkllj)\
**Post date:** [August 21, 2014, 8:37pm UTC](https://community.wolfram.com/t/at-what-level-does-replaceall-pattern-match-for-rule-execution/3050/1 "2014-08-21T20:37:27Z")

</div>

As a Mathematica beginner reading through Paul Wellin’s Mathematica book, I ran across a very interesting example

```
 {{3, 4}, {7, 2}, {1, 5}} /. {x_, y_} :> {y, x}

```

which returns the elements of each ordered pair exchanged resulting in {{4, 3}, {2, 7}, {5, 1}}

I thought it would be interesting to generalize it for handling “rows” with two or more columns without regard to how many columns each row had (or the number of rows in the list) and using a rule something like

```
  {x_, y_, z___} :>{y, x, z}

```

But, I quickly ran into a problem which has caused me to realize I do not understand what Mathematica is going to do in general. Not only did my rule not work, it failed in a way not predictable to me. Simplifying problem with Wellin’s example modified to be a 2 x 2 matrix instead of a 3 x 2 matrix:

```
  {{3, 4}, {7, 2}} /. {x_, y_} :> {y, x}

```

results in {{7, 2}, {3, 4}}

That is, Mathematica no longer exchanges the elements within each sublist, it exchanges the sublists. This is confusing to me because I expected if a rule works with a simplified sample problem it will work with generalized versions. That seems not to be the case.

Why is Mathematica doing this and how should be expression be coded to work as I had expected?

---

<div class="post-metadata">

**Author:** ![nma](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@nma](https://community.wolfram.com/u/nma)\
**Post date:** [August 21, 2014, 8:55pm UTC](https://community.wolfram.com/t/at-what-level-does-replaceall-pattern-match-for-rule-execution/3050/2 "2014-08-21T20:55:59Z")

</div>

If you want to always replace at second level only, you can use

```
       Replace[{{3, 4}, {7, 2}, {1, 5}}, {x_, y_} :> {y, x}, {2}]
       (* {{4, 3}, {2, 7}, {5, 1}} *)
       Replace[{{3, 4}, {7, 2}}, {x_, y_} :> {y, x}, {2}]
       (* {{4, 3}, {2, 7}} *)

```

The reason why `ReplaceAll[{{3, 4}, {7, 2}}, {x_, y_} :> {y, x}]` produced `{{7, 2}, {3, 4}}` and not `{{4, 3}, {2, 7}}` is because Mathematica matched `{x_,y_}` with `{3, 4}, {7, 2}` as a _whole_, hence `x_` matched `{3, 4}` and `y_` matched `{7, 2}` and so it returned these flipped (ReplaceAll stops once it finds a match and starts from first level). In the first case `ReplaceAll[{{3, 4}, {7, 2}, {1, 5}}, {x_, y_} :> {y, x}]` it worked, since Mathematica tried first to match at _first level_, and could not (not possible to match `{a,b,c}` with `{x_,y_}`, so it went to second level, and found the pattern it can match to, and hence it worked then.

So if you want to always match at specific level, you can use `Replace` with specific level and be explicit about it. Look up help for `Replace`. It makes a difference if ones uses level `{n}` vs. `n` also. `{n}` says only level `n`, while `n` means level 1 to `n`.

---

<div class="post-metadata">

**Author:** ![winkllj](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@winkllj](https://community.wolfram.com/u/winkllj)\
**Post date:** [August 21, 2014, 9:39pm UTC](https://community.wolfram.com/t/at-what-level-does-replaceall-pattern-match-for-rule-execution/3050/3 "2014-08-21T21:39:41Z")

</div>

Thank you. I kind of expected an answer similar to yours. This leads me to another question, which was the critical one lurking but unstated.

How do you Mathematica Pros write bullet-proof code? ReplaceAll (/.) and ReplaceRepeated (//.) are used extensively in the sample code I’ve seen, but my simple example suggests to me that I have to exercise tight control over data structures submitted to my own processes in order to prevent possibly undetected and absurd results.
