# Why no do-blocks for regex matching

**URL:** https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445
**Category:** Internals & Design
**Tags:** regex
**Created:** [January 11, 2022, 10:46pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445 "2022-01-11T22:46:07Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [January 11, 2022, 10:46pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/1 "2022-01-11T22:46:07Z")

</div>

I’m just wondering why the standard library doesn’t include a method for [`match`](https://docs.julialang.org/en/v1/base/strings/#Base.match) defined as

```julia
function match(f::Function, r::Regex, args...)
    m = match(r, args...)
    if !isnothing(m)
        f(m)
    end
end

```

This allows to write

```julia
match(rx, string) do m
    @show m
end

```

as opposed to

```julia
m = match(rx, string)
if !isnothing(m)
    @show m
end

```

A do-block for regex matching feels like a very natural pattern (one that Python nicely enabled with the [“walrus operator”](https://www.python.org/dev/peps/pep-0572/#syntax-and-semantics) in Python 3.8).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 11, 2022, 10:52pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/2 "2022-01-11T22:52:20Z")

</div>

> [@goerz](#):
>
> A do-block for regex matching feels like a very natural pattern (one that Python nicely enabled with the [“walrus operator”](https://www.python.org/dev/peps/pep-0572/#syntax-and-semantics) in Python 3.8).

You can employ syntax almost identical to the linked Python example’s in Julia, because Julia’s assignment operator (`=`) has always behaved much like Python’s new-fangled `:=` in that it returns its right-hand side. e.g.

```julia
if (m = match(rx, str)) !== nothing
    @show m
end

```

(That being said, Python currently allows `:=` in more contexts than Julia, e.g. Julia doesn’t allow assignments inside array literals.)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 11, 2022, 10:58pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/3 "2022-01-11T22:58:24Z")

</div>

In general, Julia’s standard library typically supports `do` block syntax for functions that naturally accept function arguments (e.g. `map`), functions that require resource cleanup (e.g. `open`), and for cases that the user cannot emulate efficiently/concisely with a sequence of separate calls (e.g. `get!` on a dictionary).

`match` seems to fall into none of those categories. Even without putting the assignment into the `if`, there’s nothing particularly bad about puting `m = match(...)` on a separate line from `if isnothing(m)` as far as I can tell.

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [January 11, 2022, 11:16pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/4 "2022-01-11T23:16:26Z")

</div>

Well, I would argue for the “conciseness” case, but fair enough 😉

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [January 11, 2022, 11:27pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/5 "2022-01-11T23:27:41Z")

</div>

The most immediate answer to your “why” question is that nobody has tried to make it happen (write a PR, advocate for it, successfully get it merged). If they had, we’d be able to find/cite a PR with reasons for or against. It’s not terribly unlike this idea for `replace`, though ([#24598](https://github.com/JuliaLang/julia/issues/24598)) — and there you’ll find little excitement and some other concerns.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 12, 2022, 1:17am UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/6 "2022-01-12T01:17:12Z")

</div>

> [@mbauman](#):
>
> It’s not terribly unlike this idea for `replace` , though ([#24598](https://github.com/JuliaLang/julia/issues/24598))

That case is quite different: `replace` already accepts a function argument, so the proposal was merely to move it to the first argument to exploit `do` syntax.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 13, 2022, 6:18pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/7 "2022-01-13T18:18:39Z")

</div>

I quite like this syntax but I think it would be clearer to introduce an `ifmatch` function:

```julia
ifmatch(re, str) do m
    # body
end

```

A downside of this compared to using an actual if statement is that there’s no way to add an else clause, but that may be fine.

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [January 13, 2022, 7:23pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/8 "2022-01-13T19:23:01Z")

</div>

> [@stevengj](#):
>
> (That being said, Python currently allows `:=` in more contexts than Julia, e.g. Julia doesn’t allow assignments inside array literals.)

The syntax is a little bit tricky, but assignment inside array literals does seem to work:

```julia-repl
julia> xyz
ERROR: UndefVarError: xyz not defined

julia> [(xyz = 2;), 3]
2-element Vector{Int64}:
 2
 3

julia> xyz
2

```

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [January 13, 2022, 8:25pm UTC](https://discourse.julialang.org/t/why-no-do-blocks-for-regex-matching/74445/9 "2022-01-13T20:25:02Z")

</div>

What about a higher-order function like [`when-let`](https://riptutorial.com/common-lisp/example/9863/if-let--when-let---foo--let-macros)?

```julia
function when_let(body, val)
    if val !== nothing
        body(val)
    end
end

```

That would be a bit more general, and apply to any function that uses `Nothing` as a sentinel value:

```julia
julia> when_let(match(r"\d+", "a42b")) do m
           println(m.match)
       end
42

julia> a = collect(11:15);
julia> when_let(findfirst(iseven, a)) do i
           println("$i => $(a[i])")
       end
2 => 12

```
