# Is there way to replace n occurrences starting from the end of a string?

**URL:** <https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 7, 2018, 6:47pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999 "2018-10-07T18:47:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [October 7, 2018, 6:47pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/1 "2018-10-07T18:47:24Z")

</div>

Let’s say you have:

```julia
foo = "fizz_buzz_fizz_buzz_fizz_buzz_fizz_buzz_fizz_buzz"

```

and you want to change the last two buzzes to bars:

```julia
nu_foo = "fizz_buzz_fizz_buzz_fizz_buzz_fizz_bar_fizz_bar"

```

What is the easiest way to do this with the least code.

* * *

For clarity, this would be the opposite of the current replace function with an limit count given:

```julia
julia> replace(foo, "buzz", "baz", 2)

"fizz_baz_fizz_baz_fizz_buzz_fizz_buzz_fizz_buzz"

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 7, 2018, 7:41pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/2 "2018-10-07T19:41:44Z")

</div>

In case nothing better is found, a rookery of reversions could do the trick

```julia
 reverse(replace(reverse(foo), reverse("buzz") => reverse("baz"), count=2))
"fizz_buzz_fizz_buzz_fizz_buzz_fizz_baz_fizz_baz"

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 7, 2018, 8:00pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/3 "2018-10-07T20:00:19Z")

</div>

I think this is the way to go, even if there’s a function for OP, probably would just be a wrapper

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [October 7, 2018, 8:10pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/4 "2018-10-07T20:10:38Z")

</div>

Yup, that’s the way to do it:

```julia
function rreplace(varargs...; kwargs...) 
    varargs = map(
        vararg -> isa(vararg, AbstractString) ? reverse(vararg) : vararg,
        varargs
    )
    
    reverse(replace(varargs...;kwargs...))
end

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 7, 2018, 8:37pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/5 "2018-10-07T20:37:32Z")

</div>

be careful:

```julia
julia> rreplace(foo, r"b.zz"=>"baz", count=2)
"fizz_buzz_fizz_buzz_fizz_buzz_fizabuzz_fizabuzz"

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [October 7, 2018, 8:46pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/6 "2018-10-07T20:46:21Z")

</div>

I’ll leave that as an exercise for the reader

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 8, 2018, 3:49pm UTC](https://discourse.julialang.org/t/is-there-way-to-replace-n-occurrences-starting-from-the-end-of-a-string/15999/7 "2018-10-08T15:49:24Z")

</div>

Another option, more concise and efficient than the previous:

```julia
julia> join(rsplit(foo, "buzz", limit=3), "baz")
"fizz_buzz_fizz_buzz_fizz_buzz_fizz_baz_fizz_baz"

```

According to the documentation, this solution should also support regexes, but the code for that seems to be missing.
