# String substitution with interpolation

**URL:** <https://discourse.julialang.org/t/string-substitution-with-interpolation/24949>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [June 5, 2019, 6:35am UTC](https://discourse.julialang.org/t/string-substitution-with-interpolation/24949 "2019-06-05T06:35:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 5, 2019, 6:35am UTC](https://discourse.julialang.org/t/string-substitution-with-interpolation/24949/1 "2019-06-05T06:35:00Z")

</div>

Hello, I’d like to have something like:

```julia
foo(rs::String, t::String) = replace(rs, r"(aa|bb)" => s"\1$(t)")

```

Note the replacement string uses both a substitution (`\1`) and an interpolation (`$t`). This does not seem to be allowed and I’m not sure what my options are short of doing a match and writing the replacement myself manually.

Ideally the function would do:

```julia
foo("aac", "dd") == "aaddc"
foo("bbe", "ff") == "bbffe"

```

Am I missing something? is there a natural way to do this? thanks!

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 5, 2019, 6:36am UTC](https://discourse.julialang.org/t/string-substitution-with-interpolation/24949/2 "2019-06-05T06:36:57Z")

</div>

Ok well I should have thought for two minutes more, using the `SubstitutionString` constructor does what I want. I’ll leave this here lest it should be useful for someone else:

```julia
foo(rs::String, t::String) = replace(rs, r"(aa|bb)" => SubstitutionString("\\1$(t)"))

```
