# Destructuring NamedTuples with Regular Expressions

**URL:** <https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509>\
**Category:** General Usage\
**Tags:** destructuring\
**Created:** [October 10, 2022, 7:00am UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509 "2022-10-10T07:00:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [October 10, 2022, 7:00am UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/1 "2022-10-10T07:00:08Z")

</div>

As of Julia 1.7, one can deconstruct NamedTuples. I wanted to know if it’s possible to use regular expressions to do this, rather than specifying the variables to be deconstructured.

MWE:

```julia
x = (; a11=1, a12=2, b1=3)

#the next is the feature I was looking for
(; r"^a") = x

```

right now, the only way I found is

```julia
(; a11, a12) = x

```

but I have a lot of variables and wanted to keep the code clean when I unpack the parameters.

Thanks!!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 10, 2022, 1:00pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/2 "2022-10-10T13:00:28Z")

</div>

one thing to consider is whether you want to use a namedtuple of arrays here by which I mean `(a=[1,2], b=3)`.

---

<div class="post-metadata">

**Author:** ![chris-b1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-b1/32/14165_2.png) [@chris-b1](https://discourse.julialang.org/u/chris-b1)\
**Post date:** [October 10, 2022, 2:57pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/3 "2022-10-10T14:57:18Z")

</div>

My guess/opinion is that something like would be considered magical for base julia.

That said, you absolutely could write a macro to implement that functionality

```julia
macro regex_destruct(ex)
     # match regex against field names from right hand side
     # generate assignment statements for new names
     # wrap in esc(...) so new variables don't get gensym names
end

@regex_destruct (; r"^a") = x

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 10, 2022, 7:51pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/4 "2022-10-10T19:51:45Z")

</div>

It is not clear to me the advantage of having the possibility of using regex to select all the variables of interest all at once, when then in the code they must still be “used” one by one by hand.  
In any case, just to experience the possibilities of metaprogramming …

```julia
julia> x = (; a11=1, a12=2, b1=3, ba1=4)
(a11 = 1, a12 = 2, b1 = 3, ba1 = 4)
# ff=filter(f->startswith(string(f),"a"), propertynames(x))
julia> gg=filter(f->!isnothing(match(r"^a", string(f))), propertynames(x))
(:a11, :a12)

julia> eval.(Expr.(:(=) , gg,getproperty.(Ref(x),gg)))
(1, 2)

julia> a11
1

julia> a12
2

```

ps  
in @chris-b1 answer I would like to understand the use and necessity of escape(…)  
(this is my second time I try to do some kind of metaprogramming)

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [October 10, 2022, 8:16pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/5 "2022-10-10T20:16:19Z")

</div>

Thanks for your answers! It seems that there only “hacks” to do this, then? I saw that Parameters.jl allows you to unpack all variables in one line, which is ultimately what I’m interested in. But this is only valid for `struct`, not for NamedTuples.  
I just wanted to avoid adding the following line for each function I’m using

```julia
(; a11, a12) = x

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 10, 2022, 8:22pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/6 "2022-10-10T20:22:23Z")

</div>

fwiw, I find `@unpack a11, a12 = x` than `(; a11, a12) = x`

But ultimately, _please_ write out the variables explicitly. Code which creates global variables without actually writing out the literal is very hard to read and maintain. Better for the future to get used to writing things out.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [October 10, 2022, 8:41pm UTC](https://discourse.julialang.org/t/destructuring-namedtuples-with-regular-expressions/88509/7 "2022-10-10T20:41:28Z")

</div>

Yeah, I thought about whether it’s more convenient keeping the variables explicit. But sometimes you have around 20 parameters that use in every single function. I use Greek letters for those, so there’s no risk of confusion. It’s just becomes annoying to have one line unpacking the same variables in every function.

It’s the first time I’m working on a big project, so still learning how to deal this.I should probably inspect the possibility of using `const` now that it ensures type-stability in the new versions of Julia. Maybe I’ll create a PR to see if this feature is worth being added.

Btw, I use `(; a11, a12) = x` rather than ` @unpack a11, a12 = x` because I think that `Parameters` will be eventually deprecated? See [here](https://github.com/mauro3/UnPack.jl/issues/14). Even if it’s not deprecated, I want to be on the safe side and use Base functionality.
