# RegexMatch to NamedTuple?

**URL:** <https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551>\
**Category:** New to Julia\
**Tags:** strings, regex, namedtuple\
**Created:** [March 30, 2019, 9:20pm UTC](https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551 "2019-03-30T21:20:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 30, 2019, 9:20pm UTC](https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551/1 "2019-03-30T21:20:17Z")

</div>

When parsing a string with regex like `m = match(r"^(?<str_a>\S+) (?<str_b>\S+)$", "abc def")` it is possible to get group values like `m[:str_a]` and so on. But how do I convert the match to namedtuple, `(str_a = "abc", str_b = "def")` in this case? I cannot find a way to do this in the documentation, or for that matter cannot find how to convert a regex match to any plain data structure like dict.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 30, 2019, 11:58pm UTC](https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551/2 "2019-03-30T23:58:48Z")

</div>

i check the source code, and the symbol calling uses a transformation to indexes. I mean, the natural representation of the RegexMatch is with number indices. i think that providing to the regex the symbols and building the dict or tuple from there is a possible solution, but its very interesting a convert to build a convert to that (maybe file a bug on github?). An option that can work is using those indices to build the tuple or dict. Here are some humble functions to build a dict or NamedTuple from a RegexMatch:

```julia
m1 = match(r"^(?<str_a>\S+) (?<str_b>\S+)$", "abc def")

function regex_to_dict(x::T) where T<:RegexMatch
return Dict{Int64,String}(i=>x.captures[i] for i = 1:length(x.captures))
end

function regex_to_dict(x::T,keys) where T<:RegexMatch
return Dict{eltype(keys),String}(keys[i]=>x.captures[i] for i = 1:length(x.captures))
end

function regex_to_ntuple(x::T) where T<:RegexMatch
    return NamedTuple{Tuple(collect(1:length(x.captures)))}(Tuple(x.captures))
end

function regex_to_ntuple(x::T,keys) where T<:RegexMatch
    return NamedTuple{Tuple(keys[1:length(x.captures)])}(Tuple(x.captures))
end

julia> regex_to_dict(m1,(:str_a,:str_b))
Dict{Symbol,String} with 2 entries:
  :str_a => "abc"
  :str_b => "def"

julia> regex_to_ntuple(m1,(:str_a,:str_b))
(str_a = "abc", str_b = "def")

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 31, 2021, 9:54am UTC](https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551/3 "2021-12-31T09:54:55Z")

</div>

In case anyone encounters this topic looking for a solution now, a recent Julia version made this conversion somewhat easier:

```julia
NamedTuple{Symbol.(Tuple(keys(m)))}(values(m))

```

to namedtuple, and

```julia
Dict(pairs(m))

```

to dict.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 20, 2023, 5:42pm UTC](https://discourse.julialang.org/t/regexmatch-to-namedtuple/22551/4 "2023-08-20T17:42:28Z")

</div>

Every time I need this conversion, I either try to remember or lookup the solution above. So that it’s not needed anymore, I made a PR [convert RegexMatch to Dict and NamedTuple by aplavin · Pull Request #50988 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/50988) that adds `Dict`/`NamedTuple` constructors converting `RegexMatch` to the corresponding container.  
Hope this small but convenient functionality gets added soon!
