# Using replace with a function of the match

**URL:** <https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264>\
**Category:** New to Julia\
**Created:** [June 12, 2020, 12:57pm UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264 "2020-06-12T12:57:34Z")\
**Posts on this page:** 5\
**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 12, 2020, 12:57pm UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264/1 "2020-06-12T12:57:34Z")

</div>

Hello,

It’s easy to recuperate the matched substring in `replace` and do something like:

```julia
julia> replace("foobar", r"b([a-z])r" => s"\1")
"fooa"

```

However I can’t pass the output to a function so for instance this:

```julia
julia> replace("foobar", r"b([a-z])r" => uppercase(s"\1"))
"foo\\1"

```

this works but is a bit silly with the double matching:

```julia
julia> rx = r"b([a-z])r"
julia> replace("foobar", rx => s -> uppercase(match(rx, s).captures[1])
"fooA"

```

is there an obvious way to do this better? generally what might be nice is to do something like:

```julia
replace("foobar", regex => myfun) 

```

where the `myfun` would get access to the regex match and have access to fields like `captures`

```julia
myfun(m) = uppercase(m.captures[1])

```

Thanks!

cc: @Wikunia

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [June 12, 2020, 2:55pm UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264/2 "2020-06-12T14:55:02Z")

</div>

But `replace("foobar", regex => myfun)` already has a meaning: it replaces the matched substring (i.e. `match(rx, "foobar").match`) by the result of applying `myfun` to it (if the matched string exists).

Using this interface to apply the function to the matched string makes sense, and is simpler to reason about. E.g., what should be done if there are various captured substrings? Maybe apply `myfun` to all? Ok, that might make sense.

Now then, what if the captured substrings are nested into each other? Well, `uppercase` may be an easy one for this, but there may be others less clear, e.g. `strip`, etc.

---

<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 12, 2020, 3:45pm UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264/3 "2020-06-12T15:45:24Z")

</div>

So I agree that the change I’m suggesting is breaking and so would not happen between 2.0 if at all.

Note that I disagree it’s ambiguous; the current behaviour in “my” perspective amounts to passing `m.match`; if you have access to `m` you have strictly more information than in the current case (you have the match, you have the groups for the `SubstitutionString` and you could apply a function to any of these things).

I might just copy the code from `replace` and implement a `replace_transform` or something like it.

PS: I hope it’s clear that the `uppercase` example is just a toy example to clarify my use case.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 12, 2020, 3:50pm UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264/4 "2020-06-12T15:50:45Z")

</div>

It’s not impossible to allow for something like this in a non-breaking way. You could introduce a wrapper `SubstitutionFunction`, which would wrap a function and if used on the right hand side of `replace` together with a regex, it gets passed the matches instead of the whole string.

---

<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 15, 2020, 8:58am UTC](https://discourse.julialang.org/t/using-replace-with-a-function-of-the-match/41264/5 "2020-06-15T08:58:54Z")

</div>

Opened [https://github.com/JuliaLang/julia/issues/36293](https://github.com/JuliaLang/julia/issues/36293) for further discussion, depending on feedback I’ll give it a shot
