# Applying a series of regular expression replacements

**URL:** https://discourse.julialang.org/t/applying-a-series-of-regular-expression-replacements/14908
**Category:** General Usage
**Tags:** question
**Created:** [September 13, 2018, 1:53pm UTC](https://discourse.julialang.org/t/applying-a-series-of-regular-expression-replacements/14908 "2018-09-13T13:53:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [September 13, 2018, 1:53pm UTC](https://discourse.julialang.org/t/applying-a-series-of-regular-expression-replacements/14908/1 "2018-09-13T13:53:59Z")

</div>

As described in [r - Automation of conversion from .Rnw to .Rmd - Stack Overflow](https://stackoverflow.com/questions/52303539/automation-of-conversion-from-rnw-to-rmd) I want to take a `R no-web` file and apply a series of regular expression replacements to it to create a `R markdown` file. Suppose that I had a vector of pairs of the form `r"foo" => s"bar"`. What would be a good method of applying them in sequence to a `String` so that the value of the `n`th `replace` call becomes the first argument to the `n+1`st call?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 13, 2018, 2:34pm UTC](https://discourse.julialang.org/t/applying-a-series-of-regular-expression-replacements/14908/2 "2018-09-13T14:34:31Z")

</div>

Maybe a for-loop?

```julia
const REPLACEMENTS = [
    r"foo" => s"baz"
    r"bar" => s"buz"
    r"buz" => s"inga"
]

function do_replace(s)
    for rs in REPLACEMENTS
        s = replace(s, rs)
    end
    return s
end

```

```julia
julia> do_replace("foobar")
"bazinga"

```

---

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [September 13, 2018, 2:40pm UTC](https://discourse.julialang.org/t/applying-a-series-of-regular-expression-replacements/14908/3 "2018-09-13T14:40:14Z")

</div>

Thanks. That is more-or-less the approach I settled on.
